Four startup modes in Activity

In Android, each interface is an Activity, and the switching interface operation is actually an instantiation operation between multiple different Activities. In Android, the startup mode of an Activity determines the startup mode of an Activity.

  There are four kinds of startup modes of Android total Activity:

  1. Activity startup mode settings:  
  2.   
  3.         <activity android:name=".MainActivity" android:launchMode="standard" />  
  4.   
  5. There are four startup modes of Activity:  
  6. . standard  
  7.   
  8.         Mode startup mode, an activity is created every time an activity is activated and placed in the task stack.  
  9. . singleTop  
  10.   
  11.         If there is an instance of the Activity on the top of the stack of the task, the instance will be reused, otherwise a new instance will be created and placed on the top of the stack (even if the Activity instance already exists in the stack, as long as it is not on the top of the stack, an instance will be created) .  
  12. . singleTask  
  13.   
  14.         If there is already an instance of the Activity in the stack, reuse the instance (the instance's onNewIntent() will be called). Reuse will bring the instance back to the top of the stack, so the instances above it will be removed from the stack. If the instance does not exist on the stack, a new instance will be created and placed on the stack.   
  15. . singleInstance  
  16.   
  17.         Create the Activity instance in a new stack and let multiple applications share the Activity instance in the new stack. Once the instance of the Activity that changes the mode exists in a certain stack, any application will reuse the instance in the stack when the Activity is activated again. The effect is equivalent to that of multiple applications sharing an application. in application.  

Where standard is the default startup mode of the system.

 

  The following is an example to demonstrate the operating mechanism of standard:

  1. private TextView text_show;  
  2.      private Button btn_mode;  
  3.        
  4.      @Override  
  5.      public void onCreate(Bundle savedInstanceState) {  
  6.          super .onCreate (savedInstanceState);  
  7.          setContentView(R.layout.activity_main);  
  8.            
  9.          text_show = (TextView) this.findViewById(R.id.text_show);  
  10.            
  11.          text_show.setText(this.toString());  
  12.            
  13.          btn_mode = (Button) this.findViewById(R.id.btn_mode);  
  14.            
  15.      }  
  16.        
  17.     // button click event  
  18.      public void LaunchStandard(View v){  
  19.          startActivity(new Intent(this,MainActivity.class));  
  20.            
  21.          text_show.setText(this.toString());  
  22.      }  

The initialization interface is as follows:

  

  When the button is clicked, a new Activity will be created. It can be seen from the display of the hexadecimal number after the TextView@. The two clicks are as follows:

  

   

  

  At this point, we analyze the operating mechanism inside the stack:

   (in order from the top of the stack up)

 

  Therefore, this Standard mode is to create a new Activity object every time. When the back button is clicked, it will destroy the top of the stack (the current Activity), and then jump to the next level. For example, if the current Activity is 44ed8c50, then when we When you click back, the Activity will become 44f28a48, but when you click the button again in this Activity to create an object, it will create a new Activity object. This mode may not be what we need in most cases, because of the impact on system performance. Excessive consumption.

  Below we introduce two startup modes that can use the Activity in the current stack:

  2. singleTop

    It can be known from the above explanation that each time a new Activity is used, it will automatically detect whether the current Activity at the top of the stack is the Activity that needs to be referenced, and if so, it will directly refer to this Activity without creating a new Activity.

    We added a "start singletop mode" button to the interface just now. When clicked, the singletop we created appears. There is a button in the Activity singletop to start the singletop mode, which means to start the current Activity, because we configure the Activity in the manifest file. The startup mode is singleTop, so it will not be created at this time but will use the singleTop Activity at the top of the current stack:

  1. <activity  
  2.             android:name=".SingleTopActivity"  
  3.             android:label="@string/singletop"  
  4.             android:launchMode="singleTop" >  
  5. </activity>  

    Interface initialization:

      

    Click the "Start singleTop mode" button:

          

  We analyze its operating mechanism, and we can see that when the program runs to this point, the data in the stack is in the form of:

    

    When we click the "Start singleTop mode" button in the above interface, since the startup mode set by this Activity is singleTop, it will first detect whether the current stack top is the Activity object we want to request, and it is verified, so it will not Create a new Activity, but refer to the Activity at the top of the current stack.

       

    Although it does not create a new Activity object, it calls the onNewIntent() method every time:

  1. @Override  
  2.      protected void onNewIntent(Intent intent) {  
  3.          // TODO Auto-generated method stub  
  4.          super.onNewIntent(intent);  
  5.            
  6.          Toast.makeText(thisnew Date().toString(), 1).show();  
  7.      }  

We write code for this method to output the current date, then every time the above button is clicked, the current date will be output.

 

  3.singleTask

    The difference between this startup mode and singleTop can be seen in the name, that is, singleTop only detects whether the Activity at the top of the current stack is what we need to request to create each time, while singleTask will detect all Activity objects in the stack, from top to bottom, If it is detected that it is what we requested, the object above the Activity object will be destroyed, and the detected Activity we need will be directly placed on the top of the stack.

    We create a SingleTaskActivity, this interface contains a start MainActivity and start SingleTaskActivity button.

  initialization:

    

  Click the "Start singleTask mode" button:

    

  In this interface, click the second button "Start singleTask mode" button. According to the definition, it will detect whether there is this Activity object in the current stack, so the current Activity is displayed and will not be recreated;

  Then click the "Start Standard Mode" button. Since the startup mode of MainActivity is standard, a MainActivity object will be recreated here:

    

  At this time, the data format in the stack is:

    

  When clicking the "Start singleTask mode" button in the above interface, since it is detected that the second Activity in the current stack is the Activity we want to create, the top MainActivity will be destroyed, and then SingleTaskActivity will be set to the top of the stack:

    

 

  4.SingleInstance

    This startup mode works similarly to the browser we use. We all know that when accessing the browser in multiple programs, if the current browser is not open, the browser will be opened, otherwise it will be accessed in the currently opened browser. This mode will save a lot of system resources, because it can ensure that there is only one Activity object to be requested in the current stack.

 

    

    The above are the four startup modes in Android, which we often use when developing Android projects. Cleverly setting the startup mode of Activity will save system overhead and program running efficiency.



Original URL: http://blog.csdn.net/knlnzhao/article/details/8005277

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324124236&siteId=291194637