Four kinds of launchmode for Android activity!!!

Source: Internet
Author: User

This article turns from: http://marshal.easymorse.com/archives/2950. Write very good, share to everyone!!!

In multi-activity development, it is possible that activity jumps between applications, or the reusable activity of other applications. You might want 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.

Load pattern classification and where to configure

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:

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

You can also edit in the Eclipse ADT graphical interface:

The load pattern of the activity is distinguished by a glance at the example. Here is an example of a cyclic jump of activity A (ActA) and Activity B (ACTB). The difference between the four modes can be explained by slightly altering the loading mode and the code.

Standard

First of all, the standard mode, also is the default mode, do not need to configure Launchmode. Write an activity named Acta first:

[Java]View Plaincopy
  1. Package com.easymorse.activities;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.view.View;
  6. Import Android.view.View.OnClickListener;
  7. Import Android.widget.Button;
  8. Import Android.widget.LinearLayout;
  9. Import Android.widget.TextView;
  10. Public class ActA extends Activity {
  11. /** Called when the activity is first created. * /
  12. @Override
  13. public void OnCreate (Bundle savedinstancestate) {
  14. super.oncreate (savedinstancestate);
  15. TextView TextView = new TextView (this);
  16. Textview.settext (This+ "");
  17. Button button = New button (this);
  18. Button.settext ("Go ActA");
  19. Button.setonclicklistener (new Onclicklistener () {
  20. @Override
  21. public void OnClick (View v) {
  22. Intent Intent = new Intent ();
  23. Intent.setclass (ActA. This, ActA.  class);
  24. StartActivity (Intent);
  25. }
  26. });
  27. LinearLayout layout = new LinearLayout (this);
  28. Layout.setorientation (linearlayout.vertical);
  29. Layout.addview (TextView);
  30. Layout.addview (button);
  31. This.setcontentview (layout);
  32. }
  33. }

There is no layout in the example to avoid looking at the wordy. Visible is an example of acta–> acta. The ToString value of the object is printed in the interface to identify whether a new acta instance is created based on the hash code.

First interface:

After clicking the button:

Can be a few more times. Discovery creates a new instance of the activity every time. The standard loading mode is like this, and the intent will be sent to the new instance.

Now click the Back button of the Android device, you can see is in accordance with the previous creation of the activity instance of the reverse order, similar to the operation of the fallback, and just the operation of the Jump button is the operation of the pressure stack. Such as:

Singletop

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.

Or with just the example, just change the launchmode to Singletop, and you'll see the difference.

When running, you will find that by how many times the button is the same Actia instance, because the instance is at the top of the stack and therefore does not create a new instance. If fallback, the app is exited.

Singletop mode, which can be used to solve the problem that the stack repeats the same activity at most.

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 B activity jumps to a activity, because the top of the stack is not a Activity instance.

The Acta class changes slightly:

[Java]View Plaincopy
  1. Package com.easymorse.activities;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.view.View;
  6. Import Android.view.View.OnClickListener;
  7. Import Android.widget.Button;
  8. Import Android.widget.LinearLayout;
  9. Import Android.widget.TextView;
  10. Public class ActA extends Activity {
  11. /** Called when the activity is first created. * /
  12. @Override
  13. public void OnCreate (Bundle savedinstancestate) {
  14. super.oncreate (savedinstancestate);
  15. TextView TextView = new TextView (this);
  16. Textview.settext (This+ "");
  17. Button button = New button (this);
  18. Button.settext ("Go ACTB");
  19. Button.setonclicklistener (new Onclicklistener () {
  20. @Override
  21. public void OnClick (View v) {
  22. Intent Intent = new Intent ();
  23. Intent.setclass (ActA. This, ACTB.  class);
  24. StartActivity (Intent);
  25. }
  26. });
  27. LinearLayout layout = new LinearLayout (this);
  28. Layout.setorientation (linearlayout.vertical);
  29. Layout.addview (TextView);
  30. Layout.addview (button);
  31. This.setcontentview (layout);
  32. }
  33. }

ACTB class:

[Java]View Plaincopy
  1. Package com.easymorse.activities;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.view.View;
  6. Import Android.view.View.OnClickListener;
  7. Import Android.widget.Button;
  8. Import Android.widget.LinearLayout;
  9. Public class ACTB extends Activity {
  10. @Override
  11. protected void OnCreate (Bundle savedinstancestate) {
  12. super.oncreate (savedinstancestate);
  13. Button button=New button (this);
  14. Button.settext ("Go ActA");
  15. Button.setonclicklistener (new Onclicklistener () {
  16. @Override
  17. public void OnClick (View v) {
  18. Intent intent=New Intent ();
  19. Intent.setclass (ACTB. This, ActA.  class);
  20. StartActivity (Intent);
  21. }
  22. });
  23. LinearLayout layout=New LinearLayout (this);
  24. Layout.addview (button);
  25. This.setcontentview (layout);
  26. }
  27. }

The ACTB class uses the default (standard) load and acta is loaded with Singletop. The results are similar:

If you change the Acta loading mode to standard, the same is true.

Singletask

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.

The above singletop in the example of Acta Launchmode changed to SINGLETASK,ACTB to standard. Then you will find that you press the button once in the Acta interface:

Then press the button in the ActB1 interface, because Acta is singletask and will use the original ActA1 instance. The situation in the stack at this time:

If you press the button multiple times, you will find that there is always only one instance of ActA1 this acta class.

SingleInstance

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.

If you still take the example of Acta and ACTB, you can change the ACTB mode to singleinstance,acta as standard, if you press the button once to switch to ACTB, see the phenomenon similar to this:

If it is the first time the button switches to ACTB, the ACTB at the press of the button to switch to Acta, and then fallback, is:

In addition, you can see that the taskid of two activity is different.

Four kinds of launchmode for Android 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.