Activity four loading modes in Android

Source: Internet
Author: User

Activity four modes of loadingwe know that when configuring activity, you can specify the Android:lauchmode property, which is used to configure the load mode for the activity, and the overview line supports the following four types:
1.Standard: Normal mode, this is the default load mode.
2.singletop: Task top singleton mode.       3.singletask: Single-instance mode in task.
4.singleinstance: Global singleton mode.


So why does the activity need to develop a loading pattern ?
since an app is launched on Android, the system automatically creates a task that belongs to the app, or it can be understood as the activity stack, the activity is created at the bottom of the stack, created at the top of the stack, While Android does not provide us with an API for working with tasks, we cannot directly manipulate the task, but we can control the load relationship between the activity and the task by controlling the load mode of the activity.

1. Standard mode
activity initiated in this mode creates a new activity instance and joins the existing task stack, which does not start the new one.
public class Mainactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        LinearLayout ll = new LinearLayout (this);        Ll.setorientation (linearlayout.vertical);        This.setcontentview (ll);        TextView TV = new TextView (this);        Tv.settext ("activity is:" + this.tostring ()                + "\ n" + ", Task ID:" + this.gettaskid ());        Button button = New button (this);        Button.settext ("Activate activity");        Ll.addview (TV);        Ll.addview (button);        Button.setonclicklistener (                new View.onclicklistener () {                    @Override public                    void OnClick (View v) {                        Intent Intent = new Intent (mainactivity.this, mainactivity.class);                        StartActivity (intent);}}        );}    }

Because this is the default load mode for activity, it is no longer necessary to androidmanifest.xml the file. Each time a button is clicked, a new activity is generated and added to the stack, and after generating multiple, press the back key repeatedly. You can see that the activity in the stack shows up one by one. Careful observation of the hashcode values of each activity will be different, but the taskid are the same, stating that no new task will be created.


2.singletop Mode
The activity created by this pattern is also placed directly on top of the stack, unlike standard, except that the Singletop mode detects if the current top activity is the same class as the activity that is currently required to start, and if it is the same class, The system will directly reuse the activity already owned, not the same class, and will create an activity on top of the stack as in standard mode.


The code is exactly the same as above, and you need to add android:launchmode= "Singletop" to the Androidmanifest.xml


no matter how the button is clicked,,,, will not react.


3.singletask Mode
Singletask Mode when the activity is loaded, only one instance of the activity is allowed in the same task. When you create it, there are three scenarios to consider:
1. If an instance of the activity does not exist in the task, create a join
2. If the task exists and is exactly at the top of the stack, it will be the same as the Singletop mode operation, no response.
3. If there is a task and is not at the top of the stack, the activity above the activity in the task is ejected first and placed on top of the stack.

note make the appropriate changes in the Androidmanifest.xml file.
public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {s        Uper.oncreate (savedinstancestate);        LinearLayout ll = new LinearLayout (this);        Ll.setorientation (linearlayout.vertical);        This.setcontentview (LL);        TextView TV = new TextView (this);        Tv.settext ("activity is:" + this.tostring () + "\ n" + ", Task ID:" + this.gettaskid ());        Button button = New button (this);        Button.settext ("Activate activity");        Ll.addview (TV);        Ll.addview (button); Button.setonclicklistener (New View.onclicklistener () {@Override Pub                        LIC void OnClick (View v) {Intent Intent = new Intent (mainactivity.this, Secondactivity.class);                    StartActivity (Intent);    }                }        ); }}public class Secondactivity extends Actionbaractivity {@Override protected voidOnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        LinearLayout ll = new LinearLayout (this);        Ll.setorientation (linearlayout.vertical);        This.setcontentview (LL);        TextView TV = new TextView (this);        Tv.settext ("activity is:" + this.tostring () + "\ n" + ", Task ID:" + this.gettaskid ());        Button button = New button (this);        Button.settext ("Activate activity");        Ll.addview (TV);        Ll.addview (button); Button.setonclicklistener (New View.onclicklistener () {@Override Pub                        LIC void OnClick (View v) {Intent Intent = new Intent (secondactivity.this,mainactivity.class);                    StartActivity (Intent);    }                }        ); }}

Clicking the button in the first activity launches the second activity, and clicking the button in the second activity pops the second activity and shows the first activity. Press the Back button in the first activity, Will exit the program directly, which means the second activity disappears. If you press the back key in the second activity, you will be returned to the first activity.



4.singleinstance Mode
This loading pattern will be significantly different from the previous ones, and the system guarantees that the target activity will be created from any task, and that the intelligence creates a target activity instance and uses a completely new task stack to hold the instance. In the creation of the following two cases:
1. If no instance of the target activity exists in the task, the target activity instance is created and stored in a separate task.  2. There are instances in the task, regardless of which task (either the task created later or the task that was originally created by the main program), the system will transfer that task to the foreground to display it.

public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {s        Uper.oncreate (savedinstancestate);        LinearLayout ll = new LinearLayout (this);        Ll.setorientation (linearlayout.vertical);        This.setcontentview (LL);        TextView TV = new TextView (this);        Tv.settext ("activity is:" + this.tostring () + "\ n" + ", Task ID:" + this.gettaskid ());        Button button = New button (this);        Button.settext ("Activate activity");        Ll.addview (TV);        Ll.addview (button); Button.setonclicklistener (New View.onclicklistener () {@Override Pub                        LIC void OnClick (View v) {Intent Intent = new Intent (mainactivity.this, Secondactivity.class);                    StartActivity (Intent);    }                }        ); }}public class Secondactivity extends Actionbaractivity {@Override protected voidOnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        LinearLayout ll = new LinearLayout (this);        Ll.setorientation (linearlayout.vertical);        This.setcontentview (LL);        TextView TV = new TextView (this);        Tv.settext ("activity is:" + this.tostring () + "\ n" + ", Task ID:" + this.gettaskid ());        Button button = New button (this);        Button.settext ("Activate activity");        Ll.addview (TV);        Ll.addview (button); Button.setonclicklistener (New View.onclicklistener () {@Override Pub                        LIC void OnClick (View v) {Intent Intent = new Intent ();                        Intent.setaction ("Org.crazyit.intent.action.CRAZYIT_ACTION");                    StartActivity (Intent);    }                }        ); }} <activity Android:name= "Com.example.dada.testapplication.SecondActivity" Android:launchmode= "SingleInstance" android:exported= "true" android:label= "@string/title_activity_second" >                <intent-filter> <action android:name= "Org.crazyit.intent.action.CRAZYIT_ACTION"/> <category android:name= "Android.intent.category.DEFAULT"/> </intent-filter> </act Ivity>


you can see that the task ID changes after the second activity is started, and when the second activity is being displayed, clicking the button doesn't respond. Fall back to the first activity and create a second acitivity, A different value appears for the task's ID.

Once you are familiar with the four loading modes above, we can easily manage the relationship between activity and task.

Activity four loading modes in Android

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.