4 types of startup modes for Android activity (example)

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/Joanna-Yan/p/5233269.html

Let's start with Android's management of activity, Android uses task to manage multiple activity, and when we launch an app, Android creates a task for it and then launches the app's entry activity (ie < Activity in intent-filter.../> configured as Main and launcher).

Because Android does not provide an API for a task, we cannot actually access the task, only the activity's GetTaskID () method can be called to get the ID of the task it is in. In fact, we can understand the task as an activity stack, and the task will manage the activity in the form of a stack: The activity that starts first is placed at the bottom of the task stack, and the activity that starts is placed on top of the task stack.

Then the activity loading mode is responsible for managing instantiation, loading the activity, and controlling the relationship between activity and task.

The activity has 4 startup modes: Standard,singletop,singletask,singleinstance. If you want to use these four startup modes, you must configure the Launchmode property in the <activity> tag in the manifest file, such as:

<activity
Android:name= ". Mainactivity "
Android:label= "@string/app_name"
Android:launchmode= "Standard"
>

</activity>

Standard

The standard boot mode is also the default startup mode for Android. Activity initiated in this mode can be instantiated more than once, where multiple instances of activity can exist in a single task, and each instance processes a intent object. If activity A's startup mode is standard, and a is started, and activity A is started again in a, called StartActivity (New Intent (This,a.class)), an instance of a is started again on the top of a. That is, the status in the current stack is a-->a.

Singletop

If an instance of activity started in Singletop mode already exists at the top of the stack, then starting the activity again does not create a new instance, but instead reuses the instance at the top of the stack and invokes the instance's Onnewintent () The intent method passes the object to this instance. For example, if A's startup mode is Singletop, and an instance of a is already present at the top of the stack, then when you call StartActivity (new Intent (This,a.class)) to start A, the instance of a is no longer created, but the original instance is reused. and call the Onnewintent () method of the original instance. There is still an instance of a in the task stack.

If an instance of activity started in Singletop mode already exists in the task stack, but not at the top of the stack, then it behaves the same as standard mode and creates multiple instances.

Singletask

The official document says that if an activity's startup mode is Singletask, then the system will always start the activity at the bottom (root) of a new task, and other activity initiated by the activity will be present in the new task as well as the activity. If an activity already exists in the system, the instance is reused and its onnewintent () method is called. That is, there is only one instance of such an activity in the system.

However, this is not an accurate statement , the use of Singletask to start the target activity, there are three kinds of situations:

1. If the target activity to be opened does not exist, the target activity instance will be created and placed on top of the task stack.

2. If the target activity that will be started is already at the top of the task stack, the behavior is the same as the singletop mode.

3. If the target activity that is to be started already exists but is not at the top of the task stack, the system will move all the activity above the activity out of the task stack, so that the target activity moves to the top of the stack.

The following is illustrated by an example:

Two activity,firstactivity displays a text box and a button that launches the Secondactivity;secondactivity Display text box and button that is used to start firstactivity.

 Public classFirstactivityextendsActivity {TextView TV;    Button btn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.tv); Tv.settext ("Activity is:" + This. toString () + ", Task ID:" + This. GetTaskID ()); BTN=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (firstactivity. This, Secondactivity.class);            StartActivity (Intent);    }        }); }}
 Public classSecondactivityextendsActivity {TextView TV;    Button btn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_second); TV=(TextView) Findviewbyid (r.id.tv); Tv.settext ("Activity is:" + This. toString () + ", Task ID:" + This. GetTaskID ()); BTN=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (secondactivity. This, Firstactivity.class);            StartActivity (Intent);    }        }); }}

Running the instance, the system starts firstactivity by default, click the interface button, the system opens secondactivity in Singgletask mode, and there are two activity (bottom-up) in the task stack: Firstactivity-->secondactivity.

Click the Secondactivity Interface button and the system starts firstactivity again in standard mode with three activity (bottom up) in the task stack:firstactivity-->secondactivity --firstactivity.

Click the button again in the Firstactivity, the system opens the secondactivity again in Singletask mode, and the system will move all the activity that is above the secondactivity Allows the secondactivity to enter the top of the stack with only two activity (bottom-up) in the task stack:firstactivity-->secondactivity.

(In sequence):

SingleInstance

Always opens in a new task, and there is only one instance of this new task, which means that other activity initiated by the instance will automatically run in another task. When you start an instance of the activity again, the existing tasks and instances are reused. The Onnewintent () method of this instance is called and the intent instance is passed to the instance. As with Singletask, only one such activity instance will exist in the system at the same time.

Description

In this mode of loading, the system guarantees that the target activity will be started, regardless of which task it is from, only one target activity instance is created and a new task stack is used to mount the activity instance.

When you start a target activity with singleinstance, there are two different situations:
1. When the target activity that is about to start does not exist, the system creates a new task, creates an instance of the target activity, and adds it to the top of the new task.

2. If the target activity to be started already exists, regardless of which application it resides in, regardless of which task it is in, the system will take the activity's task to the foreground and use that activity to display it.

The activity loaded with this pattern is always at the top of the task stack, where the task contains only that activity.

To illustrate:

Click the Firstactivity button to start the secondactivity. Secondactivity is configured as SingleInstance load mode, the Export property is configured to True---indicates that the activity can be started by another application.

 Public classFirstactivityextendsActivity {TextView TV;    Button btn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.tv); Tv.settext ("Activity is:" + This. toString () + ", Task ID:" + This. GetTaskID ()); BTN=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (firstactivity. This, Secondactivity.class);            StartActivity (Intent);    }        }); }}
        <ActivityAndroid:name=". Secondactivity "Android:launchmode= "SingleInstance"android:exported= "true" >            <Intent-filter>                <!--know that the activity can respond to the intent of the action for the specified string -                <ActionAndroid:name= "Joanna.yan.action.TEST_ACTION" />                <categoryAndroid:name= "Android.intent.category.DEFAULT" />            </Intent-filter>        </Activity>

The system starts a new task and loads the newly created secondactivity instance with a new task, secondactivity always at the top of the stack.

Another example uses an implicit intent to start the secondactivity again.

Code:

 Public classOtheractivityextendsActivity {TextView TV;    Button btn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_second); TV=(TextView) Findviewbyid (r.id.tv); Tv.settext ("Activity is:" + This. toString () + ", Task ID:" + This. GetTaskID ()); BTN=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent intent=NewIntent (); Intent.setaction ("Joanna.yan.action.TEST_ACTION");            StartActivity (Intent);    }        }); }}

Clicking the button in Otheractivity implicitly launches the secondactivity of singleinstance mode, and if the previous example has not exited, regardless of whether the secondactivity task is in the foreground, The system will re-secondactivity the task to the foreground, thereby displaying the secondactivity.

4 types of startup modes for Android activity (example)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.