One of the four Android Models

Source: Internet
Author: User
Document directory
  • Singletop
  • Singletask
  • Singleinstance

Recently, I have been entangled in the issue of passing values between activities. What I want to do is to simply pass values, including passing values between applications and passing values in applications, with the activity Loading Mode added, this is a bit different. Let's talk about the four loading modes of activity in Android today.

Activity has four loading modes: Standard, singletop, singletask, and singleinstance. The standard mode is the default activity mode. The following describes the other three types.

Singletop

I think learning it must be done with standard. The only difference between them is that when an activity with a startup target of singletop mode already has instances at the top of the current task stack, the activity will not be re-instantiated, instead, use the activity at the top of the task stack (specific process: onresume-> onnewintent-> onresume ).

Test (because there are too many cases, the code is simple, and the code will not be pasted, it is a matter of directly logging ):

First case: calls in applications

A (main + launcher), B (singletop): A-> B -- startactivity --> B is the same task, and a B instance is not regenerated.

A (main + launcher), B (singletop): A-> B -- startactivityforresult --> B for the same task, a B instance is regenerated.

Case 2: Inter-application calls

A1 (main + launcher), A2 (main + launcher + singletop): A1-> A2 -- startactivity --> A2 same task, no re-generated into an A2 instance

A1 (main + launcher), A2 (main + launcher + singletop): A1-> A2 -- startactivity --> A2 same task, a new A2 instance will be generated

It seems that the singletop mode seems to be related to the startup mode, and has no relationship with whether it is called between applications: When started in startactivityforresult mode, it is exactly the same as the standard mode.

Singletask

It is learned together with singleinstance. Their only difference is: The singleinstance activity can only have one independent task, and the worker of this task has only one activity; singletask does not have this requirement (that is, tasks containing singletask-activity can contain many activities ). Singletask means that this activity can only have one instance and only one task. The system creates a new task and instantiates the activity at the root of the new task. however, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to itsonNewIntent()Method, rather than creating a new instance. Only one instance of the activity can exist at a time.Although the activity starts in a new task, the back key still returns the user to the previous activity. I don't think it is clear. Let's test it for the moment:

First case: calls in applications

A (main + launcher), B (singletask), C: A-startactivity-> B-> startactivityforresult-> C-startactivity-> B same task, B instance is not regenerated and C is destroyed.

A (main + launcher), B (singletask), C: A-startactivityforresult-> B-startactivityforresult-> C-startactivityforresult-> B. The same task is not regenerated, c. Destroy. 1st and 3 startactivityforresult cannot return data

The data in the image is as follows:

Case 2: Inter-application calls

A1 (main + launcher), A2 (main + launcher + singletask): A1-startActivity-> A2-startActivity-> A2 different tasks, do not regenerate A2

A1 (main + launcher), A2 (main + launcher + singletask): A1-startActivityForResult-> A2-startActivityForResult-> A2 different tasks, will not regenerate A2, the first 1, 2 startactivityforresult is invalid

It seems that no new task will be generated when the singletask mode activity is called in the application, and a new task will be generated when the singletask mode activity is called between applications. In addition, the startactivityforresult function fails to start the activity in singletask mode. In addition, when the previous singletask mode activity is reused, the activity in the middle of the task stack will be destroyed.

Singleinstance

As mentioned above, I will not talk about it here. The test result is that all tasks are different except in the application.

Note: "singleinstance" exclusively occupies a task, and other activities cannot exist in that task. If it starts a new activity, no matter what the launch mode of the new activity is, all new activities will be run in other tasks (as if the flag_activity_new_task parameter is added ). The other three modes can coexist with other activities.

 

Conclusion: The loading modes of different activities may have the expected impact if the method of starting an activity is called between applications. It's too late. Let's publish it first. It will be fixed in a few days.

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.