Talking about Android's task stack and activity startup mode

     Android's activity has 4 startup modes, but to understand the startup mode, you must first understand the concept of Android's task stack.

1. Task stack

 Each Android APP will create at least one empty task stack to store many activities. The task stack is actually the same as the stack structure in the data structure. The page loaded by the Activity at the top of the stack. By default, that is, if the user does not set it, the activity starts in Standard mode. Let's start talking about the first boot mode.

2. Startup mode

1. Standard mode

The standard mode allows repeated activities to be stored in a task stack. Every time a jump or a new activity is created, a new activity will be generated and covered on the top of the stack for the user to see. When returning to the page, the top pop-up stack is destroyed, and the user sees the second page. When the return key is pressed continuously, the stack is popped out in turn, and finally the empty stack is also recycled.

   (1) Example: A, B, and C three pages enter the stack by pressing the left, and jump to A to immediately generate an Activity overlaid on it.

   (2) Suppose C is already at the top of the stack, and jump to C at this time.

The shortcomings of this mode are also seen. If a page is displayed repeatedly, it will be very strange when the user presses the return key. Why did the C page just close, why did it appear again? Therefore it is necessary to modify the mode or close it when leaving a page.

Second, the singleTop mode. Duplicate activity instances are also allowed, divided into two cases, the page is not on the top of the stack and the page is already on the top of the stack.

(1) The page is not at the top of the stack. At this point, it is still the same as the standard to regenerate a new activity instance.

(2) The page is at the top of the stack. At this time, the activity instance at the top of the stack is directly used, and a new one will not be regenerated.

Three.singleTask mode. There will be no duplicate activity instances. In this mode, there are two cases, the case where there is an instance of this activity in the stack and the case where there is no such instance in the stack.

(1) Assuming that there are two instances of A and B in the station, jump to the A page at this time, regardless of whether the A page bit is not at the top of the stack, as long as there is an active instance of A in the stack, then all the instances above A will be destroyed. The stack, in short, let A be at the top of the stack for the user to watch.

(2) Assuming that there is no C in the stack, jumping to the C page at this time will create a new C activity instance.

Fourth, singleInstance mode. There will be no duplicate activity instances. At this time, it is special. The activity instance that holds this mode has a separate stack to store it. There is only one instance of it in the stack. If multiple applications start this activity, they share this stack. unique instance inside, no new ones are generated. The application of this mode is, for example, the lock screen page of a mobile phone.

The return mode of this mode, the pop-out sequence is CBA, the stack-in sequence is ABC, the first to appear, the last to die, the one who lives the longest, haha.

Let's talk about how to set these four modes. There are two methods. One is to set it in the manifest.xml file of android.

<</span>activity android:name=".Activity1" 

          android:launchMode="standard" 

          android:label="@string/app_name"> 

    <</span>intent-filter> 

        <</span>action android:name="android.intent.action.MAIN" /> 

        <</span>category android:name="android.intent.category.LAUNCHER" /> 

    </</span>intent-filter> 

</</span>activity

The other is to set it when starting the intent, intent.FLAG_ACTIVITY_SINGLE_TOP. I won't go into details here.

FLAG_ACTIVITY_SINGLE_TOP

 This FLAG is equivalent to the singletop in the loading mode. For example, the situation in the original stack is A, B, C, D starts D in D, and the situation in the stack is still A, B, C, D

 FLAG_ACTIVITY_CLEAR_TOP

 This FLAG is equivalent to the SingleTask in the loading mode. The Activity started by this FLAG will pop all the activities above the Activity to be started out of the stack space. Such as: the original situation in the stack is A, B, C, D, this time jump from D to B, this time the situation in the stack is A, B.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325828519&siteId=291194637