Android advanced -- Acticivity Startup Mode and acticity Startup Mode

Source: Internet
Author: User

Android advanced -- Acticivity Startup Mode and acticity Startup Mode

I. Introduction

When we start the same Activity multiple times, the system will create multiple instances by default, which looks silly, therefore, android provides the startup mode to modify the default behaviors of the system. There are currently four startup modes: standard, singleTop, singleTask, and singleInstance.

Ii. How to set the Startup Mode

1. We can use AndroidMenifest to specify the Startup Mode for the Activity.

    <activity android:name=".SecondActivity"
android:configChanges="screenLayout"
android:launchMode="singleTask"
>
</activity>

2. We can use the Intent flag to specify the Startup Mode Intent intent = new Intent () for the Activity ();
Intent. setClass (MainActivity. this, SecondActivity. class );
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // android flag
StartActivity (intent );
Iii. Introduction to Startup Mode

I saw a good blog while studying. The pictures are clear and clear, so I won't repeat it here. Attached link http://www.cnblogs.com/meizixiong/archive/2013/07/03/3170591.html

1. stardand (default ):

Here we will first look at the task stack as a stack for a simple understanding without specific analysis.

When stardand is used, every time an activity is started, it creates an instance in the stack where the activity is started.

When ApplicationContext is used to start it, the system reports an error. This sentence is not very clear when I read the book. What is the context of activity? So I checked the information. service, application, and activity are subclasses of context. I can see it here, context is not just an activity. Therefore, we can understand that the non-activity context does not have a job stack, so when they start a stardand activity, there will be no job stack, make the program report an error.

Solution: We can think that since he lacks a stack to store his tasks, we will create a stack for him. FLAG_ACTIVITY_NEW_TASK tag. However, such processing becomes a singleTask.

2. singleTop (stack top multiplexing ):

When the activity we want to launch is already at the top of the stack, the activity will not be rebuilt, and its onNewIntent method will be called back. Here we have doubts about what the onNewIntent method is. After checking the information, we found that in singleTop mode, the system does not call the onCreat () method that we usually process data, but calls another method onNewIntent (), so why do we call onNewIntent () instead of onCreat? I found many blogs and found this

Link: http://blog.sina.com.cn/s/blog_74cb7ee101017njt.html I saw for a long time, it should be called onCreat () method will create an activity instance (maybe I understand a problem, if you find out, please point out, thank you ).

Attached an example in the book

D (singleTop) ABCD --- start D ---> ABCD

D (stardand) ABCD --- start D ---> ABCDD

3. singleTask (in-stack multiplexing mode ):

In single-instance mode, as long as the activity exists in a stack, no instance is created when it is started multiple times and the onNewIntent method is called back.

For example, when A is started, the system first checks whether the stack required by A exists. (The parameter TaskAffinity is required to specify the task stack required by the activity, the default value is the package name of the application. If no package exists, create A stack, create an instance of A, and put it in the stack. If yes, check whether the instance A exists. If no, create and press it into the stack. If yes, adjust it to the top of the stack and call back the onNewIntent () method.

D (singleTask) requires S2

1. S1 ABC --> S1 ABC ---> S1 ABC

S2 D

2. S1 ABC ---> S1 ABCD

3. S1 ADBC --> S1 ADB ---> S1 AD

4. singleInstance (single instance mode)

Enhanced singleTask mode. When an activity in this mode is started, the system creates a task stack to store it. If the MainActivity instance is created in the task stack of Application 1 and the MainActivity is activated in Application 2, no creation is required. The two applications share the Activity instance.

D (singleInstance)

1. S1 ABC -- start D-> S1 ABC

S2 D

Unless S2 is destroyed, you do not need to create a new one when starting D.

So far, the startup mode has been fully introduced. The above is all my learning experience. please correct me if there is anything wrong or something bad to write. Thank you. PS. (the first time I wrote a blog, it was a bit imaginary)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.