Launchmode use of the detailed

Source: Internet
Author: User

Launchmode is a very basic but also easy to overlook problem, a high-performance mobile app is not only the code to complete the very good, but also includes the rational use of launchmode. In an application, which activity should always keep an instance, which can be instantiated more than once, and which should be shared with other applications, let's analyze it slowly.

1.standard

Standard is the normal boot mode and is the default startup mode, in which case a new activity instance is placed on top of the stack, regardless of the activity instance to be started in the current back stack (fallback stack). Suppose we now have two activity, one is mainactivity, the other is MainActivity2, and we start MainActivity2 from Mainactivity First, Then start MainActivity2 in MainActivity2 and see the effect:


Because the boot mode is standard, every time we open an activity, there is an activity on the back stack top, so when we open MainActivity2 multiple times, we need to click the 6 rewind button to go back to the main interface. Although the entire application has only two activity, MainActivity2 has saved multiple instances on the stack.

2.singleTop

Singletop is more interesting, when an activity's startup mode is Singletop, if the current back stack stack top is the activity to start, then no more activity instance is created, Instead, the activity at the top of the current stack is displayed directly, and the activity's Onnewintent () method is called, otherwise a new activity instance is created to be placed on top of the stack. Or the chestnut above, after changing the activity startup mode, let's see what's Different:


That way, no matter how many times we start MainActivity2, we end up with a two-click Back button to get back to the main screen, because when MainActivity2 is at the top of the stack, the system no longer creates a new MainActivity2 instance. Only the Onnewintent method of the current instance is called.

2.1 Application Scenarios

Singletop the use of scenes or more, for example, we are currently in the Activitya, when the system suddenly received a broadcast, this broadcast requires open Activitya, then this time there is no need to create a activitya on the top of the stack, Otherwise, when you exit Activitya you will find that you need to click two times to exit Activitya. There is the search function, when we click on the Search button, we will jump to the query results page, but in the query results page, in order to enable us to search, there is a search box above the page, so that if you do not use the Singletop startup mode, So when we search 10 times does it have to press 10 times to return to the main page? This is certainly not the case, and this is one of the best examples of singletop use.

3.singleTask

When an activity startup mode is set to Singletask, if you want to start Activitya, but the current back stack already exists Activitya is not on the top of the stack, this time will activitya the above activity all removed, Causes the Activitya to be at the top of the stack and also calls the Activitya Onnewintent method. In other words, for an activity, there is at most one instance of it in the entire back stack. If we have two activity, the start mode of the two activity is Singletask, start the MainActivity2 in Mainactivity, start mainactivity in MainActivity2, Then click the Rewind button, what happens? See:


Let's analyze briefly, after the application starts, there is only one mainactivity in the back stack, and when Mainactivity starts MainActivity2, there is no MainActivity2 in the stack. So will create a MainActivity2 instance placed on the top of the stack, when MainActivity2 started Mainactivity, because the stack has already mainactivity, but not the top of the stack, This time the system will be the top of the stack MainActivity2 removed (call MainActivity2 life cycle method), and then mainactivity will be on the top of the stack, this time there is only one activity in the stack, so click the Fallback button to return to the main screen of the phone.

3.1 Application Scenarios

Singletask is generally used in the main interface of the application, for most applications, when we click the Fallback button in the main interface is to exit the application, then when we first enter the main interface, the main interface is on the bottom of the stack, no matter how many activity we open, As soon as we get back to the main interface, we should use all the activity on the main interface to remove the main interface activity to the top of the stack, Instead of adding an instance of the main interface activity to the top of the stack (here's where the little friends should know how to quit an app).

4.singleInstance

SingleInstance general use is still relatively rare, singleinstance and singletask compared to, the difference is when we set an activity startup mode to SingleInstance, When this activity is started, the activity is placed in a new task and then invoked from this new task if there are other applications to use. Note that there is a big difference here is that there are two tasks in the system, if we have two activity,mainactivity and mainactivity2,mainactivity boot mode as the default mode, MainActivity2 startup mode is SingleInstance, when we start MainActivity2 from Mainactivity, then click the Home button, then go back to MainActivity2 from Task Manager, then click the Rewind button, See what's going to happen:


When we go back to MainActivity2 through the Task Manager, click on the fallback button instead of going back to mainactivity, but just quit the app, because it's an activity in the MainActivity2 stack. Then clicking the rewind button will of course exit the current app.


Well, that's all about the activity startup mode, and the use of the activity startup mode is a great way to improve application performance.





??

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Launchmode use of the detailed

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.