Overview of four startup modes of Activity in Android: androidactivity

Source: Internet
Author: User

Overview of four startup modes of Activity in Android: androidactivity

As one of the four Android components, Activity is the most basic and common component. It provides a display interface to achieve interaction with users. As a beginner, Activity must be proficient. Today, we will demonstrate through experiments to help you understand the four startup modes of Activity.

The demo is as follows:

Step 1: prepare the relevant configuration file and Activity creation (1) AndroidMainfest. xml configuration file before the experiment
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 <application 5 android: allowBackup =" true "6 android: icon =" @ mipmap/ic_launcher "7 android: label =" @ string/app_name "8 android: supportsRtl = "true" 9 android: theme = "@ style/AppTheme"> 10 <activity android: name = ". mainActivity "> 11 <intent-filter> 12 <action android: name =" android. intent. action. MAIN "/> 13 14 <category android: name =" android. intent. category. LAUNCHER "/> 15 </intent-filter> 16 </activity> 17 // the other two activity18 <activity android: name = ". main2Activity "> </activity> 19 <activity android: name = ". main3Activity "> </activity> 20 </application> 21 </manifest>
(2) Three layout files activity_main.xml (one is demonstrated here, and the other two are different in the TextView text below)
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout 3 xmlns: android = "http://schemas.android.com/apk/res/android" 4 xmlns: tools = "http://schemas.android.com/tools" 5 android: id = "@ + id/activity_main" 6 android: layout_width = "match_parent" 7 android: layout_height = "match_parent" 8 android: orientation = "vertical" 9 tools: context = "com. example. administrator. intentflags. mainActivity "> 10 <Button11 android: text =" first page "12 android: layout_width =" match_parent "13 android: layout_height =" wrap_content "14 android: onClick = "click1"/> 15 <Button16 android: text = "second page" 17 android: layout_width = "match_parent" 18 android: layout_height = "wrap_content" 19 android: onClick = "click2"/> 20 <Button21 android: text = "Third page" 22 android: layout_width = "match_parent" 23 android: layout_height = "wrap_content" 24 android: onClick = "click3"/> 25 <TextView26 android: layout_width = "match_parent" 27 android: layout_height = "match_parent" 28 // the remaining two are only different texts. 29 android: text = "first page" 30 android: textSize = "50sp"/> 31 </LinearLayout>
(3) MainActivity. java (only one is demonstrated, and the other two are identical)
1 import android. content. intent; 2 import android. support. v7.app. appCompatActivity; 3 import android. OS. bundle; 4 import android. util. log; 5 import android. view. view; 6/** 7 * Created by panchengjia on 2016/12/14. 8 */9 public class MainActivity extends AppCompatActivity {10 11 @ Override12 protected void onCreate (Bundle savedInstanceState) {13 super. onCreate (savedInstanceState); 14 // reference the corresponding layout file 15 setContentView (R. layout. activity_main); 16} 17 public void click1 (View view) {18 Intent intent = new Intent (this, MainActivity. class); 19 startActivity (intent); 20 // record Taskid for experiment description 21 Log. I ("Tag", "page 1 taskId:" + getTaskId (); 22} 23 public void click2 (View view) {24 Intent intent = new Intent (this, Main2Activity. class); 25 startActivity (intent); 26 Log. I ("Tag", "page 2 taskId:" + getTaskId (); 27} 28 public void click3 (View view) {29 Intent intent = new Intent (this, Main3Activity. class); 30 startActivity (intent); 31 Log. I ("Tag", "Page 3 taskId:" + getTaskId (); 32} 33}
Step 2: StandardDemo experiment in (standard) mode by default

Default Mode. You do not need to write configurations. In this mode, a new instance is created by default. Therefore, in this mode, you can have multiple identical instances and multiple identical activities.

The operation result is as follows:

The log diagram is as follows (the rollback log is not recorded ):

By analyzing the operation diagram and log, we can see that this Standard mode creates a new Activity object every time. When you click the return button, it will eliminate the top stack (current Activity, then jump to the next layer. This mode may not be required in most cases, because it consumes too much system performance.

 

Step 3: Demonstration Experiment in singleTop mode (set page 2 to singleTop)

In the current task stack, determine whether the top of the stack is the current Activity. If yes, use it directly. If not, create a new Activity and put it on the top of the stack.

The following code configures AndroidMainfest. xml:
1 <activity android:name=".Main2Activity"2             android:launchMode="singleTop"> </activity>
The operation result is as follows:

The log diagram is as follows (the rollback log is not recorded ):

The difference between this startup mode and singleTop is displayed by name. That is, singleTop only checks whether the Activity at the top of the current stack needs to be created, singleTask detects all Activity objects in the stack. From top to bottom, if we detect that the Activity object is requested, the objects above the Activity object will be eliminated, directly set the detected Activity to the top of the stack.

Step 4: Demonstration Experiment in singleTask mode (set page 2 to singleTask)

In the current task stack, check whether there is an Activity in the stack. If no Activity exists, create a new Activity to go to the stack. If yes, all the activities on the Activity will be cleared from the stack, displays the current Activity.

The following code configures AndroidMainfest. xml:
1     <activity android:name=".Main2Activity"2             android:launchMode="singleTask"> </activity>
The operation result is as follows:

The log diagram is as follows (the rollback log is not recorded ):

There is only one instance. If the Activity does not exist when you start the Activity in the same application, a new instance is created in the current task. If the Activity exists, the destory of other activities in the task is deleted and its onNewIntent method is called. If you start it in another application, a new task is created and the Activity is started in the task. singleTask allows other activities to coexist with it 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.

Step 5: Demonstration Experiment in singleInstance mode (set page 2 to singleInstance)

Create a new job stack and put it into the newly created Activity. This job stack only allows one Activity instance. if it already exists, it switches to the job stack.

The following code configures AndroidMainfest. xml:
1 <activity android:name=".Main2Activity"2             android:launchMode="singleInstance"> </activity>
The operation result is as follows:

 

The log diagram is as follows (the rollback log is not recorded ):

The working principle of the browser is similar. We all know that when you access the browser in multiple programs, if the current browser does not open, open the browser; otherwise, access will be made in the current browser. This mode saves a lot of system resources because it ensures that only one Activity object to be requested exists in the current stack.

 

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.