There are multiple activities in a project, and the task stack is used to store the created activity instances, and the task stack is a "LIFO" stack structure. For a chestnut, if we start the same activity multiple times, the system will create multiple instances into the task stack, and when the back key is returned, each time a activity out of the stack, until the stack is empty, when there is no activity in the stack, the system will recycle the task stack.
In this example, the activity does not set the startup mode, you will find that the same activity started multiple times, and the system has created a number of instances, wasted memory, this situation Android long for us to consider. Android provides 4 startup modes for activity creation, and depending on the actual scenario, different startup modes are selected for activity, minimizing the pressure to create a new activity in the stack each time, reducing memory usage.
Specific instructions and usage scenarios for the startup mode? Here are some answers to this article blog.
I. Android boot mode
1. Standard mode
Description: The default mode when Android creates activity, default to Standard mode if no startup mode is set for activity. Each time you start an activity, you re-create a new instance into the stack, regardless of whether the instance exists.
life cycle: as shown above, the life cycle of each instance created is typical, and its oncreate, OnStart, and Onresume are called.
Example: at this time in the activity stack with a, B, c three activity, at this time C is at the top of the stack, the boot mode is standard mode . If you add a click event to C activity, you need to jump to another type of C activity. The result is another C activity that goes into the stack and becomes the top of the stack.
2. singletop Stack top multiplexing mode
Note: There are two processing situations: the activity that needs to be created is already at the top of the stack, and the activity at the top of the stack will be reused, and no new activity will be created, if the activity that needs to be created is not at the top of the stack. A new activity stack is recreated as in standard mode.
life cycle: If the activity at the top of the stack is directly reused, its oncreate, OnStart will not be called by the system, as it does not change, but a new method onnewintent Callback (this method is not called back when activity is created normally).
Example: at this time in the activity stack with a, B, c three activity, at this time C is at the top of the stack, the boot mode is singletop mode . Scenario One: Adding a click event to C activity requires jumping to another type of C activity. The result is a direct reuse of the C Activity at the top of the stack. Scenario Two: Adding a click event in C activity, you need to jump to another a activity. The result is creating a new activity into the stack, which becomes the top of the stack.
3. singletask Stack internal multiplexing mode
Note: If the activity that needs to be created is already in the stack, no new activity will be created at this time, but the rest of the activity on the existing stack is destroyed, making it the top of the stack.
life cycle: same as in Singletop mode, only re-callback onnewintent method in activity
Example: at this time in the activity stack with a, B, c three activity, at this time C is at the top of the stack, the boot mode is singletask mode . Scenario One: Adding a click event to C activity requires jumping to another type of C activity. The result is a direct C Activity with the top of the stack. Scenario Two: Adding a click event in C activity, you need to jump to another a activity. The result is a activity above the B, c all destroyed, so that a activity becomes the top of the stack.
4. SingleInstance Single Instance mode
Description: SingleInstance is a special, global singleton pattern, a reinforced singletask model that, in addition to having all of its features, reinforces the point that an activity with this pattern can only be located on a single task stack. This is commonly used in the system of applications, such as launch, lock screen key application, etc., the entire system only one! So in our application is generally not used, understand can.
Example: A activity is this mode, after starting a, the system will create a separate task stack for it, due to the characteristics of the reuse of the stack, subsequent requests will not create new activity, unless this unique task stack is destroyed by the system.
two. How to use the startup mode
1. Specify activity startup mode in Manifest.xml
A statically specified method that declares the activity in the Manifest.xml file while specifying its startup mode, so that the activity is created according to the specified pattern when jumping in the code. Examples are as follows:
<activity android:name="..activity.MultiportActivity" android:launchMode="singleTask"/>
2. When you start activity, specify the startup mode in intent to create activity
A dynamic startup mode that, after new intent, dynamically specifies a startup mode by means of the intent Addflags method. Examples are as follows:
Intent intent = new Intent(); intent.setClass(context, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent);
Note: both of these methods can specify the startup mode for activity, but there are differences.
(1) Priority : Dynamic designation means that the second is higher than the first priority, if both exist at the same time, in the second way prevail.
(2) Limited scope : The first method cannot specify the flag_activity_clear_top identity directly for the activity, and the second method cannot specify singleinstance for the activity mode.
three. The Flags of the Activity
The marker bit can set the activation mode of the ACTIVITY, as described above, dynamically specify the startup mode, such as flag_activity_new_task and flag_activity_single_top . It can also affect the operational state of the ACTIVITY, such as flag_activity_clean_top and flag_activity_exclude_from_recents . Here are a few of the main mark bits, do not rote, understand a few can, when needed to check the official documents.
1. Flag_activity_new_task
The effect is to specify the "singletask" startup mode for the activity, as specified in the Androidmainfest.xml.
2. Flag_activity_single_top
The effect is to specify the "singletop" startup mode for the activity, as specified in the Androidmainfest.xml.
3. Flag_activity_clean_top
Activity with this tag bit will be launched with other activity on the same task stack, usually with the Singletask startup mode. It will do the singletask, but in fact the Singletask startup mode has the function of this tag bit by default
4.flag_activity_exclude_from_recents
Activity with this tag bit does not appear in the list of historical activity, using the scenario: when we do not want the user to return to activity through the history list, this marker bit reflects its effect. It is equivalent to specifying the properties of the activity in XML:
android:excludeFromRecents="trure"
Four. Actual application scenarios for startup mode
The standard mode in these four modes is the most common one, there is no special attention, and singleinstance mode is the whole system of single-case mode, in our application is generally not applied to, so, here is the specific explanation Singletop and application Scenarios for Singletask mode:
1. Application Scenarios for Singletask mode
The most common scenario is to keep only one instance of the activity after our app is opened, and the most typical example is the home page shown in the app. Assuming that the user jumps to another page on the homepage, performs multiple operations and wants to return to the home page, if you don't use the Singletask mode, you'll see the page more than once in the process of clicking back, which is obviously not a reasonable design.
2. Application Scenarios for Singletop mode
If you start the same type of activity in your current activity, it is recommended to specify the startup mode of this type of activity as singletop, which can reduce activity creation and save memory!
3. Note: Life cycle callbacks when reusing activity
There is also a need to consider an activity jump when carrying the page parameters of the problem .
Because when a singletop or Singletask mode is set for an activity, jumping to this activity occurs when the original activity is reused. The OnCreate method for this activity will not be executed again! The OnCreate method is only executed the first time the activity is created.
The general OnCreate method will be the data initialization of the page, UI initialization, if the page display data independent of the page jump pass parameters, you do not have to worry about this problem, if the page display data is obtained through the Getinten () method, then the problem will appear: Getinten () Always get the old data, can not receive the jump when the new data sent! Below, an example is given to illustrate:
Manifest.xml <activity android:name=".activity.CourseDetailActivity" android:launchMode="singleTop" android:screenOrientation="portrait" />
Public class coursedetailactivity extends baseactivity{......@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.activity_course_detail_layout); InitData (); Initview (); }//Initialize data Private void InitData() {Intent Intent = getintent (); Mcourseid = Intent.getstringextra (course_id); }//Initialize UI Private void Initview() { ...... } ......}
Coursedetailactivity in the above code set the startup mode in the configuration file is singletop mode, according to the introduction of the above boot mode, when the coursedetailactivity is at the top of the stack, Jump to the page again to Coursedetailactivity will be directly re-use of the original activity, and this page needs to show the data is from the Getintent () method, but the InitData () method will not be called again, the page will not be able to display the new data.
Of course this kind of situation system already thought for us, then we need another callback Onnewintent (Intent Intent) method , this method will pass up the newest Intent, so we can solve the above problem. The recommended approach here is to re-setintent and then reinitialize the data and UI, as shown in the following code:
/** 复用Activity时的生命周期回调*/ @Override protectedvoidonNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); initData(); initView(); }
This way, you can repeat jumps and display different content on a page.
Startup mode is actually a beginner Android will learn the knowledge point, before is also a know half understand, some knowledge point in fact with the design pattern, you do not use and just learn not to grasp the essence, only really go to use will these become your own, the article part of the content reference to the " Android Development Art Exploration ", Good book recommendation.
Recently in the use of startup mode, I would like to summarize a little, online about this kind of article there are many, but more summary summary or useful harmless.
Welcome to the wrong point ~
Hope to be of help to you:)
Android: Illustration of four startup modes and practical application scenarios