Boot mode for Android advanced--acticivity

Source: Internet
Author: User

First, Introduction

When we start the same activity multiple times, the system creates multiple instances by default, which can seem silly, so Android provides a startup mode to modify the default behavior of the system at design time. There are currently four modes of booting: Standard, Singletop, Singletask and SingleInstance.

Second, how to set the startup mode

1. We can specify the boot mode for activity via Androidmenifest

    <activity android:name= ". Secondactivity "
Android:configchanges= "Screenlayout"
Android:launchmode= "Singletask"
>
</activity>

2. We can specify the startup mode for activity by Intent flag bit Intent Intent = new Intent ();
Intent.setclass (Mainactivity.this,secondactivity.class);
Intent.setflags (intent.flag_activity_new_task);//android flag bit
StartActivity (Intent);
Iii. introduction of the starting mode

In the study to see a good blog, the picture is very clear, here will not repeat. Attached link http://www.cnblogs.com/meizixiong/archive/2013/07/03/3170591.html

1.stardand (System default):

Here we first put the task stack, directly as a stack, to simple understanding, do not do specific analysis.

When we use Stardand, each time the activity is started, he creates an instance in the stack where his activity is launched.

When we use ApplicationContext to start him, the system will error. This sentence I read the time is not particularly understand, what is non-activity context, so to check the next information, service, application and activity are the sub-class of the context, see here to understand, Context is not just a single point of activity. So the next thing is good to understand, because the non-activity context does not have a task stack, so one has to start a stardand mode of activity, it will be because there is no task stack, making the program error.

Solution, we can imagine that since he lacks a stack of tasks to store him, we'll create one for him. Specifies the flag_activity_new_task tag bit. But this way of handling his start-up became a singletask.

2.singleTop (Stack top multiplexing):

When the activity we want to start is already at the top of the stack, the activity will not be rebuilt, and his onnewintent method will be called back, and there is doubt as to what the Onnewintent method is. After checking the data, it was found that in Singletop mode, the system does not invoke the Oncreat () method that we normally process the data, but instead calls another method Onnewintent (), so why do we call Onnewintent () and Oncreat (). Method? Find a lot of blog, found this

Links: http://blog.sina.com.cn/s/blog_74cb7ee101017njt.html I've looked at it for a while, and it should be an instance of activity when I call the Oncreat () method (maybe I have a problem with my understanding, If you find out, please indicate, thank you).

Attached to a book example

D (singletop) ABCD---start d--->abcd

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

3.singleTask (in-stack multiplexing mode):

Single-instance mode, as long as the activity is present in a stack, he will not create an instance and callback the Onnewintent method if it is started multiple times.

Example: When starting a, the system will first look for a required stack (here is the need for a parameter: taskaffinity, this parameter specifies the task stack required by the activity, the default is the application of the package name), if not present a new stack, create an instance of a, and put into the stack. If present, then see if there is an instance of a, does not exist then create and press into the stack, if present, then tune to the top of the stack, callback Onnewintent () method.

D (singletask) requires S2

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

S2 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 with this mode is started, a task stack is created to hold him. If the Mainactivity instance is created in the task stack for app 1, and if app 2 also activates mainactivity, you don't need to create it, and both apps share the activity instance

D (singleinstance)

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

S2 D

Unless S2 is destroyed, there is no need to recreate it when you start D.

So far, the start mode has all been introduced, the above is all my learning experience, if there is anything wrong, or write bad place, please correct me, thank you. PS. (First time blogging, a little bit empty)

Boot mode for Android advanced--acticivity

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.