The interview series I promised is finally starting. I’m writing wherever I want to, but it must be related to the fact that I’m being interviewed.

Tell me about the four startup modes of Android

This is basically a must-ask question, just like “The Activity lifecycle.”

Many people have the misconception that knowing the launchMode doesn’t mean anything, but when I was asked this question without being prepared, I shuddered.

Those are the basics

Let’s start with some basic insights that most people probably know.

  • Standard This is the default startup mode for an Activity. Each time an Activity is activated, a new instance of the Activity is created and added to the task stack. Usage scenario: It can be used in most places.

  • SingleTop This is probably the most common launchMode. If an instance of the Activity happens to exist at the top of the task’s stack, it is reused by calling the onNewIntent() method; otherwise, a new instance is created and placed at the top, as in Standard mode. Even if an instance of the Activity already exists in the stack, A new instance is created, i.e. A ->B ->A, which is A ->B ->A, which is A ->B, which is A ->B, which is A ->B, which is A ->B, which is A ->B, which is A ->B. In a nutshell: The Activity is reused by calling onNewIntent() if and only if the Activity started is the same as the previous Activity. Usage scenario: Content interface of information reading APP.

  • The singleTask launchMode is specifically designed to address the other singleTop case above, where the onNewIntent() method is called to reuse the instance as long as an instance of the Activity already exists in the stack. When reusing, you simply put the Activity instance back to the top of the stack and remove all Activity instances above it. If no such instance exists in the stack, the same as standard mode. That is: A ->B -> C -> D ->B, then the stack becomes A ->B. A -> B -> C, the stack is still A -> B -> C. Usage scenario: the home page of the browser, or the home page of most apps.

  • SingleInstance creates an instance of the Activity in a new stack and shares the Activity instance in the stack with multiple applications. Once the Activity instance of this pattern already exists in a stack, any application that activates the Activity reuses the instance in the stack, again calling the onNewIntent() method. The effect is similar to multiple applications sharing an application, and the Activity will enter the same application no matter who activates it. However, it is worth noting that singleInstance should not be used for intermediate pages. If the user is in the middle of a page, the jump can be very uncomfortable. I have not encountered this in the actual development, but the calling page of Android system uses the same Activity for many calls.

That’s the end of the four modes of endorsing comprehension and memory, do you think that’s the end of it?

Yeah, I thought so, too.

Let’s talk about our Intent tag

In addition to setting the Android :launchMode attribute in androidmanifest.xml, we also need to understand the following Intent tags.

We can certainly choose to look at the official documentation for Tasks and Back Stack (you may need a ladder).

In Android, you can configure the launchMode in the androidmanifest.xml file with the Intent tag. To start an Activity, we need to pass an Intent. You can set the launching mode of the Activity by setting intent.setFlags (int flags).

Note that setting the Activity’s launch mode through code takes precedence over listing file Settings.

  • FLAG_ACTIVITY_NEW_TASK This flag causes the newly started Activity to create a separate Task.

  • The FLAG_ACTIVITY_CLEAR_TOP flag causes the newly launched Activity to check if it exists in the Task, and if it does, to clear the Activity above it so that it gains focus and does not re-instantiate an Activity. Typically used in conjunction with FLAG_ACTIVITY_NEW_TASK.

  • FLAG_ACTIVITY_SINGLE_TOP is the same as setting the launcherMode property to singleTop.

That seems pretty comprehensive, but you think that’s the end of it? No, the interviewer doesn’t usually ask you this question anymore. You can recite it.

What is the launchMode of your Activity?

Why do you ask?

1. Set to singleTask startup mode. When an instance of the Activity already exists, which of its callbacks will be executed? In which callback can we handle the parameters of the new Intent corband? (Start with startActivity(Intent))

When an instance of an Activity already exists on the top of the Task stack, which callback can handle the parameters of the new Intent co-band? (Clicking from the notification bar to jump to the Activity in the current Activity is the case at the top of the stack.)

These two questions, if you read the above, must be too easy for you. The interviewer just wants to see directly if you’ve actually done this setup, or if you know the onNewIntent() method exists.

Anything else you want to say?

There is.

I have seen such a situation in the article of “Brother Liu” before, and found that the code written by my former colleague caused such a problem.

StartActivityForResult Starts an Activity and executes onActivityResult() without even starting the jump.

I don’t know if anyone has had this problem, but I’m sure you’ll be scratching your head when you do. (Probably because we don’t pay much attention to the configuration manifests file, AndroidManifests. XML, in troubleshooting.)

We are in activity.javastartActivityForResult()You can see a list of instructions in the method.A lot of people have this problem becausestartActivityForResult()The launched Activity sets the singleTask launch mode.

Fortunately, Android 5.0 has fixed this problem. However, FLAG_ACTIVITY_NEW_TASK can also be set in the Intent.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);Copy the code

Note that MainActivity’s onResume() is also triggered. Because onActivityResult() regains focus when it is executed. Many people have also encountered onResume() being called for no reason, and this may be the case.

So that, in the end, we found that as long as it is not the original Activity in the same Task can produce this kind of immediate execution onActivityResult (), can also be verified from the original code, details see ActivityStackSupervisor. Java.

summary

There are many other questions you can ask about your Activity’s startup mode. STAR is recommended for your interview. Want you to master essence only actually, behind apply the apply ability that sees your individual only and innovation.