Activity launch mode (launchMode) detailed explanation

Activity launch mode (launchMode) detailed explanation

There are four Activity startup modes: standard, singleTop, singleTask, and singleInstance.

1.standard

Standard is the default startup mode of Activity. All activities will automatically use this startup mode if no explicit specification is made.

Each time you start a new Activity, it is placed at the top of the stack.

android:launchMode="standard", each click of the button will create a new Activity

Now, let's write a simple button to jump to Activity

  1. private Button button;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. Log.e( "MainActivity" ,this.toString());
  7. button = (Button)findViewById(R.id.button);
  8. button.setOnClickListener(new View .OnClickListener() {
  9. @Override
  10. public void onClick( View   view ) {
  11. Intent intent = new Intent(MainActivity.this, MainActivity.class);
  12. startActivity(intent);
  13. }
  14. });
  15. }

Although the page you jump to is the same, a new Activity is created each time you click the button in Task.

Activity creates log

Startup example diagram

2.singleTop

When the startup mode of the Activity is singleTop, and the started Activity is already at the top of the Activity stack, it is used directly.

android:launchMode="singleTop"

We create NextActivity and add button2 in MainActivity

  1. private Button button1,button2;
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. Log.e( "MainActivity" , this.toString());
  8. button1 = (Button) findViewById(R.id.button1);
  9. button2 = (Button) findViewById(R.id.button2);
  10. button1.setOnClickListener(this);
  11. button2.setOnClickListener(this);
  12.  
  13. }
  14.  
  15. @Override
  16. public void onClick( View   view ) {
  17. switch ( view .getId()){
  18. case R.id.button1:
  19. Intent intent = new Intent(MainActivity.this, MainActivity.class);
  20. startActivity(intent);
  21. break;
  22. case R.id.button2:
  23. Intent intent2 = new Intent(this, NextActivity.class);
  24. startActivity(intent2);
  25. break;
  26. }
  27. }

At this time, if you start MainActivity in MainActivity, it will only be created once in the Activity stack; but if you start NextActivity first and then start MainActivity, a new MainActivity will be created because the top of the Activity stack is NextActivity.

MainActivity ->MainActivity ->MainActivity

MainActivity -> NextActivity -> MainActivity

Startup example diagram

3.singleTask

When the launch mode of an activity is singleTask, starting the activity will check whether it already exists in the stack. If it does, all activities above it will be popped out of the stack.

android:launchMode="singleTask"

Startup example diagram

4.singleInstance

In singleInstance mode, there will be a separate back stack to manage activities. No matter which application accesses the activity, they all share the same stack, which allows other programs to call and share the activity.

android:launchMode="singleInstance"

Startup example diagram

The startup process in the figure is: Main -> New -> Next. This process is easy to understand.

The return process is: Next -> Main -> New

Obviously the order is different, why does this happen?

NextActivity and MainActivity are in the same stack. When we return, NextActivity is popped out of the stack. At this time, MainActivity still exists in stack A and is at the top of the stack, so MainActivity is seen.

When MainActivity returns, stack A is cleared, and then we see NewActivity on stack B.

After that, NewActivity is popped out of the stack, and when stack B is also empty, the App exits.

This is a brief introduction to the Activity startup mode. I hope it will be helpful to you. Please correct me if there are any deficiencies or errors.

There will be at least one update to this series every week. If you are interested, please follow it.

Learn together and improve together.

<<:  Four advantages and five applications of machine learning in the financial field

>>:  Introducing the attention mechanism into RNN to solve sequence prediction problems in five major areas

Recommend

Congratulations! The Shenzhou 17 manned spacecraft was launched successfully

On October 26, the Long March 2F Yao 17 carrier r...

Study finds COVID-19 survivors at high risk of death within 12 months

Since the outbreak of the new coronavirus pandemi...

Product Operation: How to increase repeat purchases through a points system?

When operating points , do you also encounter suc...

How to promote APP? APP promotion tips!

Mobile devices have become the most important too...

Apple releases iOS 11.4 beta 4 with information sync feature

Recently, Apple pushed iOS 11.4 beta 3 to develop...

MH370 found? Mystification or technological breakthrough?

More than seven years later, news about Malaysia ...

Cisco announces 6,000 layoffs: 8% of total employees

Late evening of August 13th, Beijing time, Cisco ...

Automation practice of emergency response in short video media processing system

background Every day, a large number of users aro...

A complete set of brand planning and promotion!

The 21st century is the Internet age. People'...