亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Dynamic setting
特別注意的坑
singleInstance返回任務棧
singleTask多任務棧啟動問題
singleTask的TaskAffinity與allowTaskReparenting參數
Home WeChat Applet Mini Program Development Let's take a look at Activity startup mode

Let's take a look at Activity startup mode

Dec 10, 2020 pm 05:10 PM
Applets

Small program development tutorialMainly introduces relevant information about Activity startup mode

Let's take a look at Activity startup mode

Recommended (free): Small program development tutorial

Preface

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.

About the task stack introduction

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.

Analysis of the four startup modes of Activity

standard

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.

singleTop

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.

singleTask

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.

singleInstance

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.

Two methods to set the startup mode

After understanding the four startup modes of the activity, let’s see how to specify the startup mode for him.

Static setting

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

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這個方法來指定啟動模式,這個方法傳入一個參數來指定啟動模式,其他的參數有:

  • FLAG_ACTIVITY_NEW_TASK:singleTask模式
  • FLAG_ACTIVITY_SINGLE_TOP:singleTop模式
  • FLAG_ACTIVITY_CLEAR_TOP:清除該活動上方的所有活動。一般和singleTask一起使用。但是如果你的啟動模式是standard,那么這個活動連他之上的所有活動都會被出棧再創(chuàng)建一個新的實例放進去。例如現在棧中是ABCD,以FLAG_ACTIVITY_CLEAR_TOP+standard模式啟動C的時候,首先清理掉ABC,是的,C也會被清理,然后再創(chuàng)建一個新的C放進去,執(zhí)行之后就是CD。

特別注意的坑

singleInstance返回任務棧

現在模擬一個場景:現在有三個活動 A,B,C。A和C的啟動模式都是standard,B的啟動模式是singleInstance。先啟動A,再啟動B,然后再啟動C。這個時候問題來了,如果我這個時候按下返回鍵,是回到B嗎?答案是回到A。再按一下呢,返回桌面嗎?答案是回到B,再按一下再回到桌面。其實不難理解。我們都知道singleInstance會創(chuàng)建一個獨立的棧,當我們啟動A的時候,A位于棧First中,啟動B的時候,就會創(chuàng)建一個棧Second并把B實例放進去。這個時候再啟動C,就會切換到棧FIrst,因為singleInstance創(chuàng)建的棧只能放一個,所以C會放到棧First中,當按下返回的時候,棧First中的活動就會依次出棧,直到全部出完,才會切換到棧Second中。所以要注意這個點。

singleTask多任務棧啟動問題

這個問題和上面singleTop的本質是一樣的。模擬一個場景:現在有兩個棧:First:ABC;Second:QWE。棧First位于前臺,棧Second位于后臺。A位于棧頂。這個時候以singleTask的模式啟動W,會發(fā)生什么樣的情況呢?首先會切換到棧Second,再把Q出棧,W提到棧頂,并執(zhí)行W的onNewIntent方法。這個時候按返回鍵就會把Second棧中的活動依次出棧,全部出完后才會切換到棧First。

singleTask的TaskAffinity與allowTaskReparenting參數

前面我們講到給singleTask模式指定要啟動的任務棧的名字,怎么指定呢?可以在AndroidManifest中指定相關的屬性,如下:

<activity android:name=".Main2Activity"
     android:launchMode="singleTask"
     android:taskAffinity="com.huan"
     android:allowTaskReparenting="true"/>

這里解釋一下這兩個參數

  • taskAffinity:指定任務棧的名字。默認的任務棧是包名,所以不能以包名來命名。
  • allowTaskReparenting:這個參數表示可不可以切換到新的任務棧,通常設置為true并和上面的參數一起使用。

我前面講到可以給singleTask的活動指定一個棧名,然后啟動的時候,就會切換到那個棧,并把新的活動放進去。但是如果設置allowTaskReparenting參數為false的話是不會切換到新的棧的。這個參數的意思是可不可以把新的活動轉移到新的任務棧。簡單點來說:當我們啟動一個singleTask活動的時候,這個活動還是留在啟動他的活動的棧中的。但是我們指定了taskAffinity這個參數,或者啟動的活動是別的應用中的活動,那么就會創(chuàng)建一個新的任務棧。如果allowTaskReparenting這個參數是true的話,那么這個活動就會放到那個新的任務棧中。這樣應該就可以明白了。所以這兩個經常是配套一起使用的。

總結

活動的啟動模式有四種,每種的功能都不一樣,可以結合具體需要去使用,但是最重點還是要了解他的實現原理,棧中是怎么變化的,這個是比較重要的。了解這個之后那些特殊情況也就很容易理解了。
上面我講的只是簡單的使用,關于活動啟動模式還有很多要了解。后續(xù)可能會解析一下,讀者也可以自行去深度了解。

相關免費推薦:編程視頻課程

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Develop WeChat applet using Python Develop WeChat applet using Python Jun 17, 2023 pm 06:34 PM

With the popularity of mobile Internet technology and smartphones, WeChat has become an indispensable application in people's lives. WeChat mini programs allow people to directly use mini programs to solve some simple needs without downloading and installing applications. This article will introduce how to use Python to develop WeChat applet. 1. Preparation Before using Python to develop WeChat applet, you need to install the relevant Python library. It is recommended to use the two libraries wxpy and itchat here. wxpy is a WeChat machine

Implement card flipping effects in WeChat mini programs Implement card flipping effects in WeChat mini programs Nov 21, 2023 am 10:55 AM

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: &lt;!--index.wxml--&gt;&l

Alipay launched the 'Chinese Character Picking-Rare Characters' mini program to collect and supplement the rare character library Alipay launched the 'Chinese Character Picking-Rare Characters' mini program to collect and supplement the rare character library Oct 31, 2023 pm 09:25 PM

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

Can small programs use react? Can small programs use react? Dec 29, 2022 am 11:06 AM

Mini programs can use react. How to use it: 1. Implement a renderer based on "react-reconciler" and generate a DSL; 2. Create a mini program component to parse and render DSL; 3. Install npm and execute the developer Build npm in the tool; 4. Introduce the package into your own page, and then use the API to complete the development.

How uniapp achieves rapid conversion between mini programs and H5 How uniapp achieves rapid conversion between mini programs and H5 Oct 20, 2023 pm 02:12 PM

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

Teach you how to use public account template messages in mini programs (with detailed ideas) Teach you how to use public account template messages in mini programs (with detailed ideas) Nov 04, 2022 pm 04:53 PM

This article brings you some related issues about WeChat mini programs. It mainly introduces how to use official account template messages in mini programs. Let’s take a look at them together. I hope it will be helpful to everyone.

Tutorial on writing a simple chat program in Python Tutorial on writing a simple chat program in Python May 08, 2023 pm 06:37 PM

Implementation idea: Establishing the server side of thread, so as to process the various functions of the chat room. The establishment of the x02 client is much simpler than the server. The function of the client is only to send and receive messages, and to enter specific characters according to specific rules. To achieve the use of different functions, therefore, on the client side, you only need to use two threads, one is dedicated to receiving messages, and the other is dedicated to sending messages. As for why not use one, that is because, only

Geographical positioning and map display using PHP and mini-programs Geographical positioning and map display using PHP and mini-programs Jul 04, 2023 pm 04:01 PM

Geolocation positioning and map display of PHP and mini programs Geolocation positioning and map display have become one of the necessary functions in modern technology. With the popularity of mobile devices, people's demand for positioning and map display is also increasing. During the development process, PHP and applets are two common technology choices. This article will introduce you to the implementation method of geographical location positioning and map display in PHP and mini programs, and attach corresponding code examples. 1. Geolocation in PHP In PHP, we can use third-party geolocation

See all articles