Recommended (free): Small program development tutorial
Usually when we start an activity, we just startActivity directly. Maybe we don’t pay attention to the startup mode of the activity. By default, The following are all started in the default startup mode. But startup mode is sometimes more important. For example, if you want an activity to be started only once and not have multiple instances, you may need to set it to singleTask mode. So it is necessary to understand these startup modes. At the same time, please note that startup mode ≠ startup mode. The startup mode refers to display startup and implicit startup. Don’t confuse them. I will have a dedicated article to explain display startup and implicit startup later.
To understand the startup mode, you must first understand the concept of the task stack. I won’t go into details about the implementation principle of the task stack here. Here I will briefly introduce what a task stack is. The activity instances we start will be placed in something called a task stack. We all know that the stack has the characteristics of "last in, first out". For example, the task stack is a badminton tube, and the activity instances are badmintons one by one. The ones put in last can only be taken out first. So when we start an app, a task stack is automatically created, and then we throw activity instances into it. When we press return to destroy the activities, these activities will come out of the task stack in turn. Of course, an app can have multiple task stacks. For example, an activity started using singleInstence is in an independent task stack. After understanding the concept of task stack, we can take a look at the four startup modes of activities.
This is the standard startup mode, and the default is this startup mode. Each time an activity in this startup mode is started, a new instance is created and placed on the stack, regardless of whether the same instance already exists in the stack. This is also the easiest to understand.
As the name suggests, the top of the stack is a single instance. What does that mean. Suppose you start an ActivityA now, but there is already an ActivityA instance on the top of the stack at this time, so at this time, no new instance will be created. But if the same instance exists off the top of the stack, a new instance will still be created. For example, the current activity in the stack is ABC, with A at the top of the stack. Then when A is started at this time, another A activity will not be created, but A's onNewIntent method will be executed; but if C activity is started at this time, since A is not C on the top of the stack, a new C instance will still be created. , the stack situation at this time is CABC.
Single task mode. This mode means that only a single instance can exist in the startup stack of the activity, whether it is at the top of the stack or not. Different from other startup modes, this startup mode can specify the stack to start. For example, there is a stack Main, but you can specify a stack name dev for activity A. Then when A is started, a stack called dev will be created. So what singleTask means is that when you start an activity with the startup mode of singleTask, if there is no identical instance in the stack, a new instance will be created and put into the stack; if the same instance exists in the specified stack, for example There is ABC in the stack, and then you start B, then you will not create a new B instance at this time, but put B on the top of the stack, push A out, and then execute B's onNewIntent method. At this time, the stack situation is BC.
Careful readers will find "top out". Yes, we all know that the stack is last-in-first-out. For example, if you put 3 badmintons in the tube, if you want to get the middle badminton, you can only pull out the top one first. The same reason , if you want to lift B to the top of the stack, then you must push A out. Many readers may mistakenly think that it is BAC after startup, but it is actually BC, because A must first pop out of the stack before B can come out. In the same way, if there is ADFBC in the stack, this startup B is also BC, and everything above is popped off the stack.
Single instance mode. This is an enhanced version of singleTask. He will create a new stack by himself and put this new instance into it, and this stack can only hold this active instance. So when this activity is started repeatedly, as long as it exists, the onNewIntent method of this activity will be called and switched to this stack, and no new instance will be created.
After understanding the four startup modes of the activity, let’s see how to specify the startup mode for him.
Static setting is to set the startup mode for specific activities in AndroidManifest. Set the launch mode by specifying the launchMode parameter for the activity. For example:
<activity android:name=".MainActivity" android:launchMode="singleInstance"/>
Dynamic setting is to specify the startup mode when starting the activity, for example:
Intent intent = new Intent(); intent.setClass(this,SecondActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
可以看到我们通过intent.addFlags这个方法来指定启动模式,这个方法传入一个参数来指定启动模式,其他的参数有:
现在模拟一个场景:现在有三个活动 A,B,C。A和C的启动模式都是standard,B的启动模式是singleInstance。先启动A,再启动B,然后再启动C。这个时候问题来了,如果我这个时候按下返回键,是回到B吗?答案是回到A。再按一下呢,返回桌面吗?答案是回到B,再按一下再回到桌面。其实不难理解。我们都知道singleInstance会创建一个独立的栈,当我们启动A的时候,A位于栈First中,启动B的时候,就会创建一个栈Second并把B实例放进去。这个时候再启动C,就会切换到栈FIrst,因为singleInstance创建的栈只能放一个,所以C会放到栈First中,当按下返回的时候,栈First中的活动就会依次出栈,直到全部出完,才会切换到栈Second中。所以要注意这个点。
这个问题和上面singleTop的本质是一样的。模拟一个场景:现在有两个栈:First:ABC;Second:QWE。栈First位于前台,栈Second位于后台。A位于栈顶。这个时候以singleTask的模式启动W,会发生什么样的情况呢?首先会切换到栈Second,再把Q出栈,W提到栈顶,并执行W的onNewIntent方法。这个时候按返回键就会把Second栈中的活动依次出栈,全部出完后才会切换到栈First。
前面我们讲到给singleTask模式指定要启动的任务栈的名字,怎么指定呢?可以在AndroidManifest中指定相关的属性,如下:
<activity android:name=".Main2Activity" android:launchMode="singleTask" android:taskAffinity="com.huan" android:allowTaskReparenting="true"/>
这里解释一下这两个参数
我前面讲到可以给singleTask的活动指定一个栈名,然后启动的时候,就会切换到那个栈,并把新的活动放进去。但是如果设置allowTaskReparenting参数为false的话是不会切换到新的栈的。这个参数的意思是可不可以把新的活动转移到新的任务栈。简单点来说:当我们启动一个singleTask活动的时候,这个活动还是留在启动他的活动的栈中的。但是我们指定了taskAffinity这个参数,或者启动的活动是别的应用中的活动,那么就会创建一个新的任务栈。如果allowTaskReparenting这个参数是true的话,那么这个活动就会放到那个新的任务栈中。这样应该就可以明白了。所以这两个经常是配套一起使用的。
活动的启动模式有四种,每种的功能都不一样,可以结合具体需要去使用,但是最重点还是要了解他的实现原理,栈中是怎么变化的,这个是比较重要的。了解这个之后那些特殊情况也就很容易理解了。
上面我讲的只是简单的使用,关于活动启动模式还有很多要了解。后续可能会解析一下,读者也可以自行去深度了解。
相关免费推荐:编程视频课程
The above is the detailed content of Let's take a look at Activity startup mode. For more information, please follow other related articles on the PHP Chinese website!