Android: singleTask in Activity startup mode (2)

Source: Internet
Author: User

Android: singleTask in Activity startup mode (2)

Let's take a look at the description of standard Startup Mode:

There is only one instance. If this Activity instance does not exist when you start it in the same application, a new instance is created at the top of the current stack. If yes, other Activity instances on the stack will be destroyed and the OnNewIntent method of the instance will be called. If it is started in another application instance, a new stack will be created, and start the Activity in the stack, and then I open the new Activity in this Activity instance. The new instance will be in a stack.

In the previous article, we have understood this sentence: there is only one instance. If this Activity instance does not exist when you start it in the same application, A new instance is created at the top of the current stack. If a new instance exists, other Activity instances on the stack are destroyed and the OnNewIntent method of the instance is called.

In this article, we will take an example to understand the following sentence: If you start it in another application instance, a new stack will be created and the Activity will be started in the stack, then I open the new Activity in this Activity instance, and the new instance will be in a stack.

Install two programs on your phone. The interface of one program (singleTask) is like this, And Activity2 is the start mode of singleTask.


The interface of another program (OpenActivity2) is as follows:

Okay, and then add the code.

SingleTask:

Xml layout file: one button is used to open MainActivity (Activity1), and the other is used to open OtherActivity (Activity2)

MainActivity and OtherActivity both use this layout file and add click events for its buttons

 
  
  
 
MainActivity (Activity1). class

 

Package com. example. singletop; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {private Button btn_open1, btn_open2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // set the title to Activity1setTitle ("I am Activity1"); btn_open1 = (Button) findViewById (R. id. button1); btn_open2 = (Button) findViewById (R. id. button2); btn_open1.setOnClickListener (this); btn_open2.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. button1: startActivity (new Intent (MainActivity. this, MainActivity. class); break; case R. id. button2: startActivity (new Intent (MainActivity. this, OtherActivity. class); break ;}}}

OtherActivity (Activity2). class
Package com. example. singletop; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. util. log; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class OtherActivity extends Activity implements OnClickListener {private Button btn_open1, btn_open2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // set the title to Activity2setTitle ("I am Activity2"); btn_open1 = (Button) findViewById (R. id. button1); btn_open2 = (Button) findViewById (R. id. button2); btn_open1.setOnClickListener (this); btn_open2.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. button1: startActivity (new Intent (OtherActivity. this, MainActivity. class); break; case R. id. button2: startActivity (new Intent (OtherActivity. this, OtherActivity. class); break ;}}// print the log for calling this method @ Overrideprotected void onNewIntent (Intent intent) {// TODO Auto-generated method stubsuper. onNewIntent (intent); Log. d ("OtherActivity", "OnNewIntent ");}}

Do not forget to configure in the AndroidManifest. xml file OtherActivity (Activity2)Information
        <activity android:launchmode="singleTask" android:name="com.example.singletop.OtherActivity">            <intent-filter>                <action android:name="android.intent.action.Activity2">                <category android:name="android.intent.category.DEFAULT">            </category></action></intent-filter>        </activity>

Then there is another program

OpenActivity2:

Layout file. There is only one button to open Activity2 in singleTask startup mode.

 
  
 
Open Activity2 with implicit intent
MainActivity. class
package com.example.openactivity2;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button btn_open2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_open2=(Button) findViewById(R.id.button1);btn_open2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent=new Intent();intent.setAction("android.intent.action.Activity2");intent.addCategory(Intent.CATEGORY_DEFAULT);startActivity(intent);}});}}
Okay.

We perform this operation.

OpenActivity-> Activity2-> Activity1-> Activity1-> Activity2

What is the situation in the stack?

Two Activity stacks are created. This is because when we open OpenActivity2, we create a stack named OpenActivity stack.

Activity2 is enabled when you click the button. Because Activity2 is the Startup Mode of singleTask, Activity2 does not enter the openActivity stack. Instead, it creates an Activity stack named SingleTask stack. After activating Activity1, openActivity stack (openActivity2), SingleTask stack (Activity1, Activiy2), Activity1, openActivity stack (openActivity2), and SingleTask stack (Activity1, Activity1, Activiy2 ). if Activity2 is enabled again because Activity2 already exists in the SingleTask stack, the above two Activity instances (Activity1, Activity1) will be destroyed and the OnNewIntent method will be called.The output information in the log is as follows:

We need to press the return key twice to return to the main interface.

Now, the singleTask startup mode is over ~

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.