SingleTask and activitysingletask in Activity Startup Mode

Source: Internet
Author: User

SingleTask and activitysingletask in Activity Startup Mode

For SingleTop mode, even if an active instance exists in the stack, the system still creates the activity if it is not at the top of the stack. If you set the startup mode to SingleTask mode, when you start the activity, the system detects instances with the activity in the stack and will not create the activity any more, instead, all the activities above the activity are pushed out of the stack, putting the activity at the top of the stack.

Similarly, paste the code to check. Create the LearnLaunchMode_SingleTask project. The Code of each part is as follows:

FirstActivity. java:

 1 public class FirstActivity extends ActionBarActivity { 2     private Button launch_second, launch_third; 3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.activity_first); 7  8         launch_second = (Button) findViewById(R.id.launch_second); 9         launch_third = (Button) findViewById(R.id.launch_third);10         launch_second.setOnClickListener(new View.OnClickListener() {11             @Override12             public void onClick(View v) {13                 Intent intent = new Intent().setClass(FirstActivity.this, SecondActivity.class);14                 startActivity(intent);15             }16         });17         launch_third.setOnClickListener(new View.OnClickListener() {18             @Override19             public void onClick(View v) {20                 Intent intent = new Intent().setClass(FirstActivity.this, ThirdActivity.class);21                 startActivity(intent);22             }23         });24         Log.i("TAG", this + " is launch");25     }26 27     @Override28     protected void onDestroy() {29         super.onDestroy();30         Log.i("TAG", this + " is destroy!");31     }32 }

SecondActivity. java:

 1 public class SecondActivity extends Activity { 2     private Button launch_first, launch_third; 3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         requestWindowFeature(Window.FEATURE_NO_TITLE); 7         setContentView(R.layout.activity_second); 8  9         launch_first = (Button) findViewById(R.id.launch_first);10         launch_third = (Button) findViewById(R.id.launch_third);11         launch_first.setOnClickListener(new View.OnClickListener() {12             @Override13             public void onClick(View v) {14                 Intent intent = new Intent().setClass(SecondActivity.this, FirstActivity.class);15                 startActivity(intent);16             }17         });18         launch_third.setOnClickListener(new View.OnClickListener() {19             @Override20             public void onClick(View v) {21                 Intent intent = new Intent().setClass(SecondActivity.this, ThirdActivity.class);22                 startActivity(intent);23             }24         });25         Log.i("TAG", this + " is launch");26     }27 28     @Override29     protected void onDestroy() {30         super.onDestroy();31         Log.i("TAG", this + " is destroy!");32     }33 }

ThirdActivity. java:

 1 public class ThirdActivity extends Activity { 2     private Button launch_first, launch_second; 3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         requestWindowFeature(Window.FEATURE_NO_TITLE); 7         setContentView(R.layout.activity_third); 8  9         launch_first = (Button) findViewById(R.id.launch_first);10         launch_second = (Button) findViewById(R.id.launch_second);11         launch_first.setOnClickListener(new View.OnClickListener() {12             @Override13             public void onClick(View v) {14                 Intent intent = new Intent().setClass(ThirdActivity.this, FirstActivity.class);15                 startActivity(intent);16             }17         });18         launch_second.setOnClickListener(new View.OnClickListener() {19             @Override20             public void onClick(View v) {21                 Intent intent = new Intent().setClass(ThirdActivity.this, SecondActivity.class);22                 startActivity(intent);23             }24         });25         Log.i("TAG", this + " is launch");26     }27 28     @Override29     protected void onDestroy() {30         super.onDestroy();31         Log.i("TAG", this + " is destroy!");32     }33 }

Activity_first.xml:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout 3 xmlns: android = "http://schemas.android.com/apk/res/android" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: orientation = "vertical"> 7 <Button 8 android: id = "@ + id/launch_second" 9 android: layout_width = "match_parent" 10 android: layout_height = "wrap_content" 11 android: text = "start the second Activity! "/> 12 <Button13 android: id =" @ + id/launch_third "14 android: layout_width =" match_parent "15 android: layout_height =" wrap_content "16 android: text = "start the third Activity! "/> 17 </LinearLayout>

Activity_second.xml:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout 3 xmlns: android = "http://schemas.android.com/apk/res/android" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: orientation = "vertical"> 7 <Button 8 android: id = "@ + id/launch_first" 9 android: layout_width = "match_parent" 10 android: layout_height = "wrap_content" 11 android: text = "start the first Activity! "/> 12 <Button13 android: id =" @ + id/launch_third "14 android: layout_width =" match_parent "15 android: layout_height =" wrap_content "16 android: text = "start the third Activity! "/> 17 </LinearLayout>

Activity_third.xml:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout 3 xmlns: android = "http://schemas.android.com/apk/res/android" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: orientation = "vertical"> 7 <Button 8 android: id = "@ + id/launch_first" 9 android: layout_width = "match_parent" 10 android: layout_height = "wrap_content" 11 android: text = "start the first Activity! "/> 12 <Button13 android: id =" @ + id/launch_second "14 android: layout_width =" match_parent "15 android: layout_height =" wrap_content "16 android: text = "start the second Activity! "/> 17 </LinearLayout>

Similarly, you also need to register SecondActivity and ThirdActivity in AndroidManifest. java, and set the Startup Mode of the preceding three activities to SingleTask:

 1 <activity 2     android:name=".FirstActivity" 3     android:launchMode="singleTask" 4     android:label="@string/app_name"> 5     <intent-filter> 6         <action android:name="android.intent.action.MAIN" /> 7         <category android:name="android.intent.category.LAUNCHER" /> 8     </intent-filter> 9 </activity>10 <activity android:name=".SecondActivity" android:launchMode="singleTask" />11 <activity android:name=".ThirdActivity" android:launchMode="singleTask" />

In this program, the system first starts FirstActivity, and the print information shows that the Activity is started. Then we click "start second Activity! "Button, you can see SecondActivity is launch in the display information; then click" Start third Activity! ", You can also see that the third activity is started. Click Start first Activity! ". Observe the print information and you can see SecondActivity is destroy, and ThirdActivity is destroy. This indicates that both SecondActivity and ThirdActivity have been out of the stack. Now you only need to press the back key once to exit the program. The sequence of other creation activities will not be demonstrated.

 

To sum up, we can draw out the principle of this startup mode as follows:

 

 

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.