Android junior developer notes - activity startup mode of learning (1)

First learning Android is a very important concept, startup mode. Article is only an introduction to the record, with the in-depth study of the activity will have to start learning mode (2) and (3). The following three dots talk about understanding the difference of the start-up mode and how to set.

Preliminaries

(1) activity is a task-stack approach to management, and the latter is advanced out of the stack data structure, the stack of activity can only change the order by way of pop and push. (2) application of a start time, the system will default to create a task stack, the default startup activity in the same task stack. When you start another application, before the task stack will be moved to the background as a background task stack, and just started the task stack was transferred to the foreground, it becomes the foreground task stack. Appear to users is always the foreground task stack stack activity.

Understanding the four startup mode

Android which said start-up mode is actually a way to start the activity. There are the following four

  • standard (default)
  • singleTop (top of the stack multiplexing)
  • singleTask (the stack multiplexing)
  • singleInstance (singleton)

standard-- default startup mode of activity, regardless of the situation at this time the task stack, each time you start an activity, the system will create an instance of it, and put it top of the stack.

singleTop-- Another common startup mode, when you start a activity, the system will check whether the activity instance already exists in the current task stack, if it already exists, you get this instance by onNewIntent put it in the stack. if there is not. We will create the activity instance placed top of the stack.

singleTask-- When starting activity in this model, you need to specify its task stack. activityA start activityB time, the system will first look for the presence of the stack.

  • It does not exist, create a stack and create activity instance in the stack.
  • It exists, to find whether there are examples of B in the stack, and if so, this example is obtained by onNewIntent As the stack. The activity will no instance is created it is placed top of the stack.

singleInstance-- would expect to see the name of the singleton pattern is not it - yes, indeed, this kind of thinking. In order to take full advantage of activity to achieve the ultimate example, after setting the startup mode, activityA start activityB, will create a special task stack is activityB, as for instance and create activityB top of the stack. Examples of the activity can be applied within the shared system.

to sum up

For an activity from time to time want to show it in front of the user, using the default: whether in the top of the stack is not, is created (there may be multiple instances of the stack) -> stack reuse: If the top of the stack, the complex with (the stack may have multiple instances, but relatively default mode, lacking) -> multiplexing the stack: the stack has no Chou Chou, have to use, not to make one (and only one instance of the stack ) -> singleton: direct their own categories, creating a stack, only one instance of this activity and the top of the stack, no matter which application can use (the application context is only one example). After several startup mode understand this, we need to decide which mode to use to start the actual situation.

Specific usage scenarios

Since I am currently using the boot mode is relatively small, the follow-up to have more to add.

  • Scene: the notification bar pop-up Notification, Notification click to jump to the specified Activity. Suppose now is at the top show in front of the user that this activity. If it is the default mode, it will open the current Activity again. This display in front of the user's results will be very strange. (It appears to be flashed) If the startup mode using singleTop, will avoid this phenomenon.

  • Scenario: Suppose we have a program that allows other programs Activity calls, we want to share examples of other programs of this Activity. If the startup mode using singleInstance, we can solve this problem.

How to set the startup mode of activity

There are two general ways to set.

  • Set in the manifast file by the launchMode activity.
  • Achieved by the activity of intent to start setting flag.

Doubts and answers

1. How to set a different task stack?

2. When activityA start in standard mode B, B instance is created and is in the top activityA task stack, so this time B started the A (standard) it?

3. What difference does it set in two ways? Commonly used flags there are several, what combination of different effects to use it?

These and other answers my next article and then continue to introduce Ha ~ stay tuned primary Android Development Notes - activity startup mode of learning (2) ~

about the author

  • Yang Xiaohua: Guangzhou reed Technology Android APP team intern

Interpolation information

Guess you like

Origin blog.csdn.net/weixin_34037515/article/details/91391975