Activity start mode and Application Scenario, activity mode scenario
Role of Startup Mode
In daily development, when we start the same Activity multiple times by default, the system creates multiple instances and puts them into the task stack one by one. When we click the back key, the system will remove an Activity from the top of the stack until the stack is empty. When there is no Activity in the stack, the system will recycle the task stack. In order to optimize the memory consumption caused by multiple instance creation when the same Activity is started for multiple times, Android provides four startup modes to modify the default behavior.
Currently, the four startup modes are standard, singleTop, singleTask, and singleInstance;
To facilitate analysis of log printing, a base class is defined:
/*** Author: hzw on * desc: easy to print basic class */public class BaseActivity extends AppCompatActivity {public static final String TAG = "BaseActivity "; private String getClassName () {return getClass (). getSimpleName () + "--" ;}@ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); if (getsuppactionactionbar ()! = Null) {getSupportActionBar (). setTitle (getClassName ();} Log. d (TAG, getClassName () + "onCreate (): taskId" + getTaskId (); dumpTaskAffinity () ;}@ Override protected void onStart () {super. onStart (); Log. I (TAG, getClassName () + "onStart ()") ;}@ Override protected void onResume () {super. onResume (); Log. I (TAG, getClassName () + "onResume ()") ;}@ Override protected void onStop () {super. onStop (); Log. I (TAG, getClassName () + "onStop ()") ;}@ Override protected void onPause () {super. onPause (); Log. I (TAG, getClassName () + "onPause ()") ;}@ Override protected void onRestart () {super. onRestart (); Log. I (TAG, getClassName () + "onRestart ()") ;}@ Override protected void onDestroy () {super. onDestroy (); Log. I (TAG, getClassName () + "onDestroy ()") ;}@ Override protected void onNewIntent (Intent intent) {super. onNewIntent (intent); Log. I (TAG, getClassName () + "onNewIntent: taskId" + getTaskId (); dumpTaskAffinity ();} protected void dumpTaskAffinity () {try {ActivityInfo info = this. getPackageManager (). getActivityInfo (getComponentName (), PackageManager. GET_META_DATA); Log. I (TAG, getClassName () + "taskAffinity:" + info. taskAffinity);} catch (PackageManager. nameNotFoundException e) {e. printStackTrace ();}}}Standard default mode
Standard is the standard mode and the default mode of the system. If you do not specify the LaunchMode, the system uses this mode by default. This mode features that a new instance is created every time an Activity is started, whether or not the Activity instance already exists. The instance of the created Activity has the same lifecycle as the normal typical Activity. For example, the onCreate, onStart, and onResume methods are called. This is a typical multi-instance implementation. A task stack can contain multiple instances. In this mode, who started the Activity, the Activity will be in the stack where the started Activity is located.
Configuration method:
You do not need to use the launchMode statement for standard mode. By default, the system is already standard.
Test Case
Enable a StandardActivity in NextActivity and start a StandardActivity again in StandardActivity.
NextActivity:
Public class NextActivity extends BaseActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_next); findViewById (R. id. bt_standard ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {// standard mode standard; Intent intent = new Intent (NextActivity. this, StandardActivity. class); startActivity (intent );}});}}
StandardActivity:
Public class StandardActivity extends BaseActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_standard); findViewById (R. id. bt_standard ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// start Intent intent Intent = new Intent (StandardActivity) continuously again. this, StandardActivity. class); startActivity (intent );}});}}
After entering StandardActivity, start StandardActivity continuously again, and then press the back key to exit.
Log output:
03-26 15:09:27.367 3628-3628/com.hzw.tapp D/BaseActivity: NextActivity--onCreate() taskId:703-26 15:09:27.367 3628-3628/com.hzw.tapp I/BaseActivity: NextActivity--taskAffinity:com.hzw.tapp03-26 15:09:27.377 3628-3628/com.hzw.tapp I/BaseActivity: NextActivity--onStart()03-26 15:09:27.377 3628-3628/com.hzw.tapp I/BaseActivity: NextActivity--onResume()03-26 15:09:37.067 3628-3628/com.hzw.tapp I/BaseActivity: NextActivity--onPause()03-26 15:09:37.077 3628-3628/com.hzw.tapp D/BaseActivity: StandardActivity--onCreate() taskId:703-26 15:09:37.077 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--taskAffinity:com.hzw.tapp03-26 15:09:37.077 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onStart()03-26 15:09:37.077 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onResume()03-26 15:09:37.447 3628-3628/com.hzw.tapp I/BaseActivity: NextActivity--onStop()03-26 15:09:44.487 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onPause()03-26 15:09:44.497 3628-3628/com.hzw.tapp D/BaseActivity: StandardActivity--onCreate() taskId:703-26 15:09:44.497 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--taskAffinity:com.hzw.tapp03-26 15:09:44.497 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onStart()03-26 15:09:44.497 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onResume()03-26 15:09:44.877 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onStop()03-26 15:37:57.387 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onPause()03-26 15:37:57.397 3628-3628/com.hzw.tapp D/BaseActivity: StandardActivity--onCreate() taskId:703-26 15:37:57.397 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--taskAffinity:com.hzw.tapp03-26 15:37:57.397 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onStart()03-26 15:37:57.397 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onResume()03-26 15:37:57.797 3628-3628/com.hzw.tapp I/BaseActivity: StandardActivity--onStop()
From the log, we can see that one NextActivity and three standardactivities are started, and the taskId of each StandardActivity is the same as that of NextActivity.Who started the Activity, then the Activity will be in the stack where the started Activity is locatedThe onCreate, onStart, and onResume methods are called each time StandardActivity is started.Each time you start an Activity, a new Activity instance is created.
SingleTop stack top reuse Mode
In this mode, if a new Activity already exists at the top of the stack, the Activity will not be re-created, and it will call the onNewIntent method, this method can be used to retrieve the information carried by the onCreate and onStart methods. If the new Activity instance already exists but is not on the top of the stack (or the new Activity does not exist in the stack ), the new Activity will still create a new instance.
Configuration method
Or:
Intent intent = new Intent(NextActivity.this, SingleTopActivity.class);intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);startActivity(intent);
Test Case