Four startup modes for "Android Notes" activity

Source: Internet
Author: User

In multi-activity development, it is possible that activity jumps between applications, or the reusable activity of other applications.

You might want an activity to jump to an activity instance instead of generating a lot of repetitive activity.

This requires that the activity be configured with a specific load pattern , rather than using the default load -mode.

There are four modes of loading for activity:

    • Standard
    • Singletop
    • Singletask
    • SingleInstance

Set the location of the activity element's Android:launchmode property in the Androidmanifest.xml file:

1 <  android:name= "ACTB"  android:launchmode= "Singletask"></  activity>

1. Standard mode

First of all, the standard mode, which is the default mode , does not need to be equipped with LaunChmode.

In this mode, if the same activity instance is created repeatedly, intent is sent to the new instance. That is, the same activity has multiple:

2. Singletop mode

Singletop and Standard mode, the intent will be sent a new instance (the latter two modes are not sent to the new instance if they already have one).

However, Singletop requires that if the stack top already has an instance of the activity to be created when the intent is created, the intent is sent to the instance without sending it to the new instance.

Singletop mode, can be used to solve the stack top multiple Repeat the same activity problem.

If a activity jumps to B activity and jumps to a activity, the behavior is the same as standard, and a new instance of activity is created when activity B jumps to a activity, because The top of the stack was not a activity instance .

3. Singletask mode

Both the Singletask mode and the subsequent singleinstance mode are created with only one instance.

When intent arrives and needs to create Singletask mode activity, the system checks to see if an instance of the activity is already in the stack. If there is a direct send intent to it.

4. SingleInstance mode

Explaining the singleinstance mode is more troublesome.

The first thing to say is the concept of task.

In the case of swing or Windows programs, there may be multiple windows that can be toggled, but you can't reuse a person's window in your own program.

Note is the direct reuse of people's binary code, not you get the others API after the source-level call.

Android can do that by letting someone else's program directly reuse your activity (like a Desktop program's window).

Android provides this mechanism by introducing the concept of task.

A task can be thought of as a stack that can be put into multiple activity. For example, to launch an app, Android creates a task and then launches the app's entry activity, which is the one configured for main and launch in Intent-filter (see an APK file deployment that results in multiple app installs).

This activity is the root activity, which may invoke other activity at its interface, which, if it follows the three patterns above, will also be in this stack (Task), only to instantiate a different strategy.

The verification method is to invoke and print the activity's TaskID:

TextView textView2 = new TextView (this);
Textview2.settext ("Task ID:" +this.gettaskid ());

Will find that the switch Activity,taskid is the same regardless.

Of course, in this single task stack, you can put in other people's activity, such as Google Maps, so that the user has seen the map press the fallback button, will be back to call the map activity. For users, they don't feel like they're manipulating multiple apps. This is the role of task.

However, with this requirement, multiple tasks share an activity (singletask is sharing an activity in a task).

The ready-made example is Google Maps. For example, I have an application that is guided by the Google Maps activity which is called. Now, for example, by pressing the home button and then going to the app list to open Google Maps, you'll find that the map you just showed is actually the same activity.

This requirement cannot be achieved if the above three modes are used. There are several contextual activity in the Google Maps app, such as Route queries, and the Guide app has some contextual activity. Fallback in their respective apps to fall back into their context activity.

The singleinstance mode solves this problem (it took a long detour to get to the chase). Let the activity in this mode stand alone in a task stack. This stack has only one activity. Both the Guide app and the intent sent by the Google Maps app are received and displayed by this activity.

Here are two more questions:

    • If this is the case, multiple task stacks can also be considered an application. For example, the Guide application launch map activity, is actually in the Guide application task stack singleinstance mode created (if not yet, if there is a direct display it) a new stack, when the only activity in this stack, map activity Back to the time, just to remove the stack, so that the guide to see the application of the activity just now;
    • Multiple applications (tasks) sharing an activity requires that none of these apps exit, such as the emphasis on using the home button to switch from the Guide app to the map app. Because if you exit the Guide app and the map app is not running, the individual map activity (task) exits.

In the following example, Mainactivity is Standard mode and subactivity is SingleInstance mode:

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

Activity Load Mode two:

Typically, an application has a task, which is a collection of activities to complete a job . And these activity is organized into a stack of forms.

When an activity is started, it is pressed into the task's stack, and when the user presses the return key in the activity, or when the code is removed, it pops out of the task's stack.

If we do not have special needs, our application will show the situation as shown:

In fact, however, our needs are far less simple than we thought. Sometimes you might want to reopen a task when you start an activity, and sometimes you might want to put an activity that already exists on top of the stack instead of recreating one.

Android in order to enable us to break the default stack's sequential pattern, Two ways are available: One is to specify its load mode when Androidmanifest.xml defines activity, and the other is to add a flag to intent when an activity is opened with intent.

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

The difference between the two approaches is that the former is to describe yourself, to other acttivity, and so on to declare how you are going to load me, while the latter is dynamic, pointing out how I want you to load (the activity to start).

Summary:

1, "Take doctrine" standard mode. Where to call me I'll go where, can be instantiated multiple times, can be several of the same activity overlap.

2, "Refuse to Stack" singletop mode. It can be instantiated multiple times, but not multiple identical activity overlaps, and the onnewintent function is called when the top of the stack is the same activity.

3, "Independent portal" Singletask mode. When the activity is invoked in the same application, if the activity is not instantiated, it is instantiated within the task of the application and, if instantiated, destroys the activity on the task and calls Onnewintent When other applications invoke the activity, if the activity is not instantiated, a new task is created and instantiated into the stack, and if it is instantiated, the activity on it is destroyed and the onnewintent is called. In a word, Singletask is the "Independent portal", in its own task, and does not allow other activity to override itself when it starts.

4, "Lonely Lonely" singleinstance mode. When the activity is loaded, if it is not instantiated, he creates a new task, instantiates it into the stack, and if it already exists, calls Onnewintent directly, and the activity's task does not allow other activity to start. Any other activity initiated from the activity will be placed in another task, first checking if there is a task for the application, and creating it without it.

Reference Original: Http://blog.csdn.net/zapzqc/article/details/8493481#t2

Four startup modes for "Android Notes" activity

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.