As one of the four components of Android, activity can be said to be the most basic and most common components, it provides a display interface, so as to achieve interaction with the user, as a beginner, must be proficient. Today we are going to demonstrate through experiments to help you understand the four starting modes of activity.
The demo works as follows:
The first step: pre-experiment preparation, related configuration files, and activity Creation (1) androidmainfest.xml configuration file
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.example.administrator.intentflags" >4<Application5Android:allowbackup= "true"6android:icon= "@mipmap/ic_launcher"7Android:label= "@string/app_name"8Android:supportsrtl= "true"9Android:theme= "@style/apptheme" >Ten<activity android:name= ". Mainactivity "> One<intent-filter> A<action android:name= "Android.intent.action.MAIN"/> - -<category android:name= "Android.intent.category.LAUNCHER"/> the</intent-filter> -</activity> - //two additional activity -<activity android:name= ". Main2activity "> </activity> +<activity android:name= ". Main3activity "></activity> -</application> +</manifest>
(2) Layout of the three activity three layouts file Activity_main.xml (here to demonstrate one, the remaining two are just the following textview text is not the same)
1<?xml version= "1.0" encoding= "Utf-8"?>2<LinearLayout3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Xmlns:tools= "Http://schemas.android.com/tools"5Android:id= "@+id/activity_main"6Android:layout_width= "Match_parent"7android:layout_height= "Match_parent"8android:orientation= "Vertical"9tools:context= "Com.example.administrator.intentflags.MainActivity" >Ten<Button Oneandroid:text= "First Page" AAndroid:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:onclick= "Click1"/> the<Button -android:text= "Second Page" -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" +android:onclick= "Click2"/> -<Button +android:text= "Third page" AAndroid:layout_width= "Match_parent" atandroid:layout_height= "Wrap_content" -android:onclick= "Click3"/> -<TextView -Android:layout_width= "Match_parent" -android:layout_height= "Match_parent" - //The remaining two characters are not the same inandroid:text= "First Page" -Android:textsize= "50sp"/> to</LinearLayout>
(3) Implementation code Mainactivity.java for three activity in Java (only one is shown, the other two are identical)
1 Importandroid.content.Intent;2 Importandroid.support.v7.app.AppCompatActivity;3 ImportAndroid.os.Bundle;4 ImportAndroid.util.Log;5 ImportAndroid.view.View;6 /**7 * Created by Panchengjia on 2016/12/14.8 */9 Public classMainactivityextendsappcompatactivity {Ten One @Override A protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - //the other two interfaces refer to their corresponding layout files here the Setcontentview (r.layout.activity_main); - } - Public voidclick1 (view view) { -Intent Intent =NewIntent ( This, Mainactivity.class); + startactivity (intent); - //record taskid for experimental instructions +LOG.I ("Tag", "Page one taskid:" +GetTaskID ()); A } at Public voidClick2 (view view) { -Intent Intent =NewIntent ( This, Main2activity.class); - startactivity (intent); -LOG.I ("Tag", "page two taskid:" +GetTaskID ()); - } - Public voidClick3 (view view) { inIntent Intent =NewIntent ( This, Main3activity.class); - startactivity (intent); toLOG.I ("Tag", "Page Three taskid:" +GetTaskID ()); + } -}Step Two: StandardDemo Experiment in default (standard) mode
The default mode, which can be configured without writing. In this mode, a new instance will be created by default. Therefore, in this mode, you can have multiple identical instances, and also allow multiple identical activity overlays.
Our operation results are as follows:
The log graph is as follows (no fallback log is logged):
By analyzing the operation diagram and log, it is concluded that the standard mode is to create a new activity object each time, when the Back button is clicked, he will destroy the top of the stack (current activity), and then jump to the next level, this mode may not be what we need in most cases, Because the performance of the system is consuming too much.
Step Three: Demo experiment in singletop mode (Setup page two is singletop)
In the current task stack, determine whether the top of the stack is the current activity, if so, it is used directly, if not, then create a new activity into the top of the stack.
The setup code in Androidmainfest.xml is as follows:
1 <activity android:name= ". Main2activity "2 android:launchmode=" Singletop "> </activity>
Our operation results are as follows:
The log graph is as follows (no fallback log is logged):
Analysis of this startup mode and Singletop in the name can see the difference, that is, singletop only detect the current stack at the top of the activity is we need to request to create, and Singletask will detect the stack of all activity objects, from the top down, If it detects what we are requesting, it will destroy the object above the activity object and put the detected activity to the top of the stack directly.
Fourth Step: Demo Experiment in Singletask mode (Setup page two is singletask)
In the current task stack, determine whether there is activity in the stack, if it does not exist, create a new activity into the stack, if present, will be the activity on top of all the activity to clear the stack, showing the current activity.
The setup code in Androidmainfest.xml is as follows:
1 <activity android:name= ". Main2activity "2 android:launchmode=" Singletask "> </activity>
Our operation results are as follows:
The log graph is as follows (no fallback log is logged):
There is only one instance. When you launch him in the same application, if the activity does not exist, a new instance is created in the current task and, if present, the other activity on top of the task is destory off and the Onnewintent method is called. If you start it in another application, a new task is created and started in that task Activity,singletask allows other activity to be stored in a task, that is, If I open a new activity in this Singletask instance, the new activity will still be in the task of the Singletask instance.
Fifth Step: Demo Experiment in singleinstance mode (Setup page two is singleinstance)
Create a new task stack, put in the newly created activity, the task stack allows only one activity instance, and if it already exists, it will switch to the task stack.
The setup code in Androidmainfest.xml is as follows:
1 <activity android:name= ". Main2activity "2 android:launchmode=" SingleInstance "> </activity>
Our operation results are as follows:
The log graph is as follows (no fallback log is logged):
Browsers work Similarly, we all know that when accessing a browser in multiple programs, if the current browser is not open, the browser opens, otherwise it will be accessed in the currently open browser. This mode saves a lot of system resources because he can guarantee that the activity object to be requested only exists in the current stack.
A brief introduction to the four starting modes of activity in Android