android Activity startup mode

Android in startup mode Detailed Activity

 

  Each interface is in a Android Activity, in fact, the switching operation is an example of the interface between a plurality of different operation Activity. Activity in Android mode determines the start up and running of the Activity mode.

  Android Activity total startup mode is divided into four categories:

Copy the code
Activity start mode setting: 

        <Android Activity: Android name = "the MainActivity.": The launchMode = "Standard" /> 

Activity four start mode: 

    1. Standard 

        mode startup mode is created every time Activity Activity activated, and placed task stack. 

    2. singleTop 

        If there is just an instance of the Activity of the top of the stack task, you reuse that instance, whether the person will create a new instance and placed top of the stack (even if the Activity stack instance already exists, if not in the stack, will create an instance). 

    3. singleTask 

        If you already have an instance of the Activity of the stack, then reuse the instance (calls onNewIntent instance ()). When reused, the instance will return to the stack, so the stack will be removed in the instance above it. If the instance does not exist in the stack, it will create a new instance placed on the stack. 

    4. singleInstance 

        create a new instance of the Activity stack, and allow multiple applications to share the Activity instance change the stack. Examples Activity Once the mode change is present in a stack, and then activates the application when any change Activity will reuse the instance of the stack, the effect is equivalent to a plurality of applications share an application, regardless of who will enter activate the same Activity applications.
Copy the code

 

  Where the standard is the system default startup mode.

 

  By the following examples to demonstrate the standard operating mechanism:

Copy the code
 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 public void LaunchStandard(View v){ 18 startActivity(new Intent(this,MainActivity.class)); 19 20 text_show.setText(this.toString()); 21 }
Copy the code

  Initialization interface as follows:

  

  When the button is clicked, creates a new Activity, by showing hexadecimal after TextView @ can see that two clicks are interface is as follows:

  

   

  

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

   (In order from the top of the stack up)

 

  Therefore, this Standard mode is created every time a new Activity object, when you click the Back button, he will be top of the stack (Current Activity) eliminated, then skip to the next level, for example if the Activity is now 44ed8c50, then when we Activity will becomes return to 44f28a48, but this time in this Activity click the button again to create an object, it will create another new Activity objects, this mode is not possible in most cases we need, because on system performance excessive consumption.

  Here we describe two can start using the current mode of the stack Activity:

  2. singleTop

    You can know from the above explanation, it will automatically detect whether it is needed Activity Activity is currently quoted at the top of the stack each time a new Activity, then if it is a direct reference to this Activity, but does not create a new Activity.

    We added a "Start singletop mode" button at just the interface appears when you click singletop we created, there is a button in the Activity singletop, start singletop mode, indicating that the startup current Activity, because we configure Activity in the manifest file the startup mode for the singleTop, so at this time but will not be created using the current top of the stack singleTop Activity:

     <activity
            android:name=".SingleTopActivity"
            android:label="@string/singletop"
            android:launchMode="singleTop" >
        </activity>

 

 

    界面初始化:

      

    点击"启动singleTop模式"按钮:

          

  我们分析它的运行机制,可知,当程序运行到此时,栈中的数据形式为:

    

    当我们在上面界面中点击"启动singleTop模式"按钮时,由于此Activity设置的启动模式为singleTop,因此它首先会检测当前栈顶是否为我们要请求的Activity对象,经验证成立,因此它不会创建新的Activity,而是引用当前栈顶的Activity。

       

    虽然它不会创建新的Activity对象,不过它每次回调用onNewIntent()方法:

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

 

    我们为此方法编写代码输出当前日期,则在每次点击上面按钮时会输出当前日期。

 

  3.singleTask

    此启动模式和singleTop在名字上即可看出区别,即singleTop每次只检测当前栈顶的Activity是否是我们需要请求创建的,而singleTask则会检测栈中全部的Activity对象,从上向下,如果检测到是我们所请求的则会消灭此Activity对象上面的对象,直接把检测到的我们需要的Activity置为栈顶。

    我们创建一个SingleTaskActivity,此界面中包含一个启动MainActivity和启动SingleTaskActivity按钮。

  初始化:

    

  点击"启动singleTask模式"按钮:

    

  在此界面中点击第二个按钮"启动singleTask模式"按钮,根据定义会检测当前栈中是否有此Activity对象,因此显示的还是当前的Activity,不会重新创建;

  再点击"启动Standard模式"按钮,由于MainActivity的启动模式为standard,所以在此会重新创建一个MainActivity对象:

    

  此时栈中数据格式为:

    

  当在上面界面中点击"启动singleTask模式"按钮时,由于检测到当期栈中第二个为我们要创建的Activity,会将最上面的MainActivity消灭,然后将SingleTaskActivity设置为栈顶:

    

 

  4.SingleInstance

    此启动模式和我们使用的浏览器工作原理类似,我们都知道在多个程序中访问浏览器时,如果当前浏览器没有打开,则打开浏览器,否则会在当前打开的浏览器中访问。此模式会节省大量的系统资源,因为他能保证要请求的Activity对象在当前的栈中只存在一个。

 

    

    Android is the top model in the four promoters, we often use to when developing Android projects, cleverly set Activity startup mode will save overhead and process efficiency.

Reproduced in: https: //www.cnblogs.com/wangzehuaw/p/4288007.html

Guess you like

Origin blog.csdn.net/weixin_33895604/article/details/93778434