[Android Note] Four startup modes of Activity: androidactivity

Source: Internet
Author: User

[Android Note] Four startup modes of Activity: androidactivity

In multi-Activity development, Activity jumps between applications or reusable activities with other applications.

You may wantAn Activity jumps to an existing Activity instance.Instead of generating a large number of repeated activities.

This requires configuring specificLoading ModeInstead of using the default loading mode.

Activity has four loading modes:

  • Standard
  • SingleTop
  • SingleTask
  • SingleInstance

The set location is in the android: launchMode attribute of the activity element in the AndroidManifest. xml file:

1 <activity android:name="ActB" android:launchMode="singleTask"></activity>

1. Standard Mode

First, the standard mode, that isDefault ModeYou do not need to configure launchMode.

In this mode, if you create the same Activity instance again, intent will send it to the new instance. That is, the same Activity has multiple:

2. singleTop Mode

Both singleTop and standard modes send intent instances (the latter two modes do not send intent instances, if they already exist ).

However, singleTop requires that if an instance of the Activity to be created at the top of the stack is created when the intent is created, the intent will be sent to the instance instead of the new instance.

SingleTop mode, available for solvingStack topMultiple duplicate Activity issues.

If A Activity jumps to B Activity and then to A Activity, the behavior will be the same as standard. A new instance of A Activity will be created when B Activity jumps to A Activity, becauseAt that time, the stack top was not A Activity instance..

3. singleTask Mode

The singleTask mode and the singleInstance mode are used to create only one instance.

When intent arrives and a singleTask Activity needs to be created, the system checks whether there are instances of the Activity in the stack. If any, send intent directly to it.

4. singleInstance Mode

It is difficult to explain the singleInstance mode.

First, let's talk about the concept of Task.

If it is a Swing or Windows program, there may be multiple Windows that can be switched, but you cannot reuse the Windows in your program.

Note that the binary code is reused directly, instead of the source code-level calling after you get the api.

Android can achieve this by allowing other programs to directly reuse your Activity (similar to windows of desktop programs ).

Android introduces the concept of Task to provide this mechanism.

A Task can be considered as a stack and put into multiple activities. For example, to start an application, Android creates a Task and starts the entry Activity of the application, the one configured as main and launch in intent-filter (see the effect of installing multiple applications in an APK file ).

This Activity is a Root Activity and may call other activities on its interface. If these activities follow the preceding three modes, they will also be in this stack (Task, the instantiation policy is different.

The verification method is to call and print the taskId of the Activity:

TextView textView2 = new TextView(this); 
textView2.setText("task id: "+this.getTaskId());

You will find that the taskId is the same no matter the Activity is switched.

Of course, you can also put other people's activities in this single Task stack, such as google map. This way, when you see the map and press the rollback key, the stack will return to the Activity that calls the map. Users do not think they are operating on multiple applications. This is the role of the Task.

However, multiple tasks share an Activity (SingleTask shares an Activity in a task.).

An example is google map. For example, I have an app for tour guides, in which google map Activity is called. Now, for example, press the home Key and open google map in the Application List. You will find that the map just now is actually the same Activity.

If the above three modes are used, this requirement cannot be met. The google map app has multiple context activities, such as route queries. The app also has some context activities. In each application, you must roll back to the corresponding context Activity.

The singleInstance mode solves this problem (the question is answered after such a long time ). Let the Activity in this mode be independent in a task stack. This stack has only one Activity. The intent sent by the tour guide application and google map application is received and displayed by this Activity.

There are two more problems:

  • In this case, Multiple task stacks can also be considered as one application. For example, when the tour guide application starts a map Activity, it is actually created in singleInstance mode on the task stack of the tour guide application (if not, it is directly displayed if there is) a new stack, when the only Activity in the stack and the map Activity are rolled back, the stack is removed, so that you can see that the tour guide has applied the Activity just now;
  • When multiple applications (tasks) share an Activity, these applications do not exit. For example, the home key is used to switch from the tour guide application to the map application. Because, if you exit the tour guide application and the map application is not running, the independent map Activity (task) will also exit.

In the following example, MainActivity is in standard mode and SubActivity is in singleinstance mode:

========================================================== ========================================================== ============

Activity Loading Mode 2:

Generally, an application has a Task,This Task is a set of activities to complete a job.. These activities are organized into stacks.

When an Activity is started, it is pushed into the stack of the Task. When you press the return key in the Activity or finish the Task in the code, it will pop up from the stack of the Task.

If we do not have any special requirements, our applications will be shown in the following figure:

However, in fact, our needs are far less simple than we thought. Sometimes, you may want to re-start a Task when opening an Activity. Sometimes you may want to put an existing Activity on the top of the stack rather than re-create one.

Android provides two methods to break the default stack generation mode: AndroidManifest. specify the loading mode when defining an Activity in xml, and add a flag to the Intent when enabling an Activity with Intent.

If both methods are used, the latter has a higher priority.

The difference between the two methods is that the former is to describe yourself and declare to other actticons how you load me; the latter is dynamic, pointing out that I want you (the Activity to be started) how to load.

 

Summary:

1. The standard mode of "tailism. Where I need to call it, I can go and instantiate it multiple times. Several identical activities can overlap.

2. "reject stack" singleTop mode. The onNewIntent function can be instantiated multiple times, but cannot overlap multiple identical activities. When the same Activity is at the top of the stack, the onNewIntent function is called.

3. "independent Portal" singleTask mode. When the Activity is called in the same application, if the Activity is not instantiated, it will be instance in the Task of the application. If the Activity has been instantiated, onNewIntent will be called after the Activity on the Task is destroyed. When other applications call the Activity, if the Activity is not instantiated, a new Task will be created and instantiated and then pushed to the stack, if it has been instantiated, the Activity on it will be destroyed and onNewIntent will be called. In other words, singleTask is an "independent Portal". In its own Task, other activities are not allowed to overwrite itself at startup.

4. singleInstance mode. If this Activity is not instantiated when it is loaded, it will create a new Task and instantiate it into the stack. if it already exists, it will call onNewIntent directly. Other activities cannot be started in the Task of this Activity, any other Activity started from this Activity will be put into other tasks. First, check whether there are tasks of this application. If not, create the Activity.

 

Http://blog.csdn.net/zapzqc/article/details/8493481#t2 ()

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.