android启动模式

​android:launchMode=

["multiple"

"singleTop" |

"singleTask"

"singleInstance"

]


  1. A。class



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    package  com.example.login;
     
    import  android.app.Activity;
    import  android.content.Intent;
    import  android.os.Bundle;
    import  android.view.View;
    import  android.view.View.OnClickListener;
    import  android.widget.Button;
     
    public  class  AActivity  extends  Activity {
    private  static  int  mIndex= 1 ;
         @Override
         protected  void  onCreate(Bundle arg0) {
             // TODO Auto-generated method stub
             super .onCreate(arg0);
             setContentView(R.layout.layout_task);
             System.out.println( "------   A activit------:" +mIndex+ "---Agettaskid--" +getTaskId());
             ++mIndex;
             Button buttonStartA = (Button) findViewById(R.id.buttonA);
             buttonStartA.setOnClickListener( new  OnClickListener() {
     
                 @Override
                 public  void  onClick(View arg0) {
                     // TODO Auto-generated method stub
                     Intent intent =  new  Intent(AActivity. this , AActivity. class );
                     startActivity(intent);
                 }
             });
             Button buttonStartB = (Button) findViewById(R.id.buttonB);
             buttonStartB.setOnClickListener( new  OnClickListener() {
     
                 @Override
                 public  void  onClick(View arg0) {
                     // TODO Auto-generated method stub
                     Intent intent =  new  Intent(AActivity. this , BActivity. class );
                     startActivity(intent);
                 }
             });
     
         }
         @Override
             protected  void  onDestroy() {
                 // TODO Auto-generated method stub
                 super .onDestroy();
     
                 System.out.println( "----activity a destory--" );
             }
    }

B。class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package  com.example.login;
 
import  android.app.Activity;
import  android.content.Intent;
import  android.os.Bundle;
import  android.view.View;
import  android.view.View.OnClickListener;
import  android.widget.Button;
 
public  class  BActivity  extends  Activity {
     private  static  int  mIndex =  1 ;
 
     @Override
     protected  void  onCreate(Bundle arg0) {
         // TODO Auto-generated method stub
         super .onCreate(arg0);
         setContentView(R.layout._layout_task);
         System.out.println( "--------   BActivity-------:"  + mIndex
                 "--TASKID:"  + getTaskId());
         ++mIndex;
 
         Button buttonStartA = (Button) findViewById(R.id.buttonA);
         buttonStartA.setOnClickListener( new  OnClickListener() {
 
             @Override
             public  void  onClick(View arg0) {
                 // TODO Auto-generated method stub
                 Intent intent =  new  Intent(BActivity. this , BActivity. class );
                 startActivity(intent);
             }
         });
         Button buttonStartB = (Button) findViewById(R.id.buttonB);
         buttonStartB.setOnClickListener( new  OnClickListener() {
 
             @Override
             public  void  onClick(View arg0) {
                 // TODO Auto-generated method stub
                 Intent intent =  new  Intent(BActivity. this , AActivity. class );
                 startActivity(intent);
             }
         });
     }
 
     @Override
     protected  void  onNewIntent(Intent intent) {
         // TODO Auto-generated method stub
         super .onNewIntent(intent);
         System.out.println( "---New Intent activity b--:"  + mIndex +  "--TASKID--"
                 + getTaskId());
     }
 
     @Override
     protected  void  onDestroy() {
         // TODO Auto-generated method stub
         super .onDestroy();
         System.out.println( "---activity b destory" );
     }
}

  1. singleTop 处在堆栈的顶端的时候只实例化一次


让B设置为singleTop

启动后 跳转到A    跳转到B  跳转到B   在跳转到B 在跳转B


1
2
3
4
5
6
11 - 07  06 : 01 : 51.371 : I/System.out( 13571 ): ------   A activit------: 1 ---Agettaskid-- 11
11 - 07  06 : 02 : 21.087 : I/System.out( 13571 ): ------   A activit------: 2 ---Agettaskid-- 11
11 - 07  06 : 02 : 24.171 : I/System.out( 13571 ): --------   BActivity-------: 1 --TASKID: 11
11 - 07  06 : 02 : 25.735 : I/System.out( 13571 ): ---New Intent activity b--: 2 --TASKID-- 11
11 - 07  06 : 02 : 26.695 : I/System.out( 13571 ): ---New Intent activity b--: 2 --TASKID-- 11
11 - 07  06 : 02 : 27.471 : I/System.out( 13571 ): ---New Intent activity b--: 2 --TASKID-- 11

不论按几下跳转B 都只显示一个 BActivity。。

但显示了三次的onNewIntent()函数的输出值 说明 此时没有执行oncreate()函数值执行了onNewIntent()函数。


现在跳转A 再按跳转B

1
2
11 - 07  06 : 04 : 34.327 : I/System.out( 13571 ): ------   A activit------: 3 ---Agettaskid-- 11
11 - 07  06 : 04 : 36.107 : I/System.out( 13571 ): --------   BActivity-------: 2 --TASKID: 11

说明在栈顶的时候无论按多少次只显示一次


结论:B不在栈顶时,依然会创建B的新实力

            当B在栈顶的时候,请求启动B的intent并没有触发创建B的    新实例,

            而是出发了栈顶B的onNewIntent();


2.SingleTask :


设置B为singleTask

启动A  启动B 启动A 启动B 这四步。

1
2
3
4
5
6
11 - 07  06 : 21 : 03.743 : I/System.out( 15039 ): ------   A activit------: 1 ---Agettaskid-- 12
11 - 07  06 : 21 : 32.283 : I/System.out( 15039 ): ------   A activit------: 2 ---Agettaskid-- 12
11 - 07  06 : 21 : 39.579 : I/System.out( 15039 ): --------   BActivity-------: 1 --TASKID: 12
11 - 07  06 : 21 : 42.619 : I/System.out( 15039 ): ------   A activit------: 3 ---Agettaskid-- 12
11 - 07  06 : 21 : 44.503 : I/System.out( 15039 ): ---New Intent activity b--: 2 --TASKID-- 12
11 - 07  06 : 21 : 44.935 : I/System.out( 15039 ): ----activity a destory--

现在栈里面 从下往上的顺序 是 

A1 A2 B1 A3  

当在次启动B的时候因为B设置了singlTask。所以不实例化B了。值执行了onNewIntent()然后把A3销毁了

结论:当B已经存在了。再次请求B会触发已经存在的B1实例的onNewIntent()

        并且如果B所在的任务栈上面有其他的Activity,那么其他的Activity也会被销毁

3.SingleInstance :

启动A 

启动B 此时新创建一个任务栈18 ,和先前的A1 并不在一个任务

启动B 没有创建新的实例

启动A  在17任务栈里面实例化一个A

1
2
3
4
5
11 - 07  06 : 53 : 34.335 : I/System.out( 17523 ): ------   A activit------: 1 ---Agettaskid-- 17
11 - 07  06 : 53 : 44.275 : I/System.out( 17523 ): ------   A activit------: 2 ---Agettaskid-- 17
11 - 07  06 : 53 : 47.043 : I/System.out( 17523 ): --------   BActivity-------: 1 --TASKID: 18
11 - 07  06 : 53 : 49.271 : I/System.out( 17523 ): ---New Intent activity b--: 2 --TASKID-- 18
11 - 07  06 : 53 : 51.571 : I/System.out( 17523 ): ------   A activit------: 3 ---Agettaskid-- 17

按下back键离栈的话顺序是

1
2
3
4
11 - 07  06 : 56 : 35.187 : I/System.out( 17523 ): ----activity a destory--
11 - 07  06 : 56 : 39.435 : I/System.out( 17523 ): ----activity a destory--
11 - 07  06 : 56 : 44.055 : I/System.out( 17523 ): ----activity a destory--
11 - 07  06 : 56 : 53.071 : I/System.out( 17523 ): ---activity b destory

先把17任务栈里面的A都销毁了。在去销毁18任务栈

singleInstance独立成立一个任务栈 这个栈里面只有B一个。始终是一个

离栈的时候先退当前的task,再退另外的task。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值