Android graphics teach you to fully understand the activity startup mode

Source: Internet
Author: User

First, we start with the simplest,

Standard

This mode is the default mode, we all know that when you use this mode, each time you send a intent, a new instance will be generated!

I write a simple example:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3  Package= "Com.example.administrator.lanuchmodetest">4 5     <Application6         Android:allowbackup= "true"7 Android:icon= "@mipmap/ic_launcher"8 Android:label= "@string/app_name"9 Android:supportsrtl= "true"Ten Android:theme= "@style/apptheme"> One         <Activity A             Android:name=". Mainactivity " - Android:label= "@string/app_name" - Android:theme= "@style/apptheme.noactionbar"> the             <Intent-filter> -                 <ActionAndroid:name= "Android.intent.action.MAIN" /> -  -                 <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> +             </Intent-filter> -         </Activity> +         <Activity A             Android:name=". Testmainactivity " at Android:label= "@string/title_activity_test_main" - Android:theme= "@style/apptheme.noactionbar"></Activity> -     </Application> -  - </Manifest>

For this app, Mainactivity is his first default boot Acitivity,testmainactivity is a button on the second activity,mainactivity Click to send intent will jump to testmainactivity,

I rewrote the onbackpressed method of testmainactivity, making it possible to press the return key on this activity without going through the system to automatically destroy the method, instead of sending intent to mainactivity to start mainactivity.

When we repeat this action several times, enter the ADB shell Dumpsys activity activities on the command line to get:

Look at this and you can clearly see that we have a task stack in this app, and all the activity in this stack is in this task, and there's a lot of repetition.

Of course this is the simplest case, and we now consider what happens if an external app launches the testmainactivity in our app?

Now I'm going to write a outerlanuchmodetest app to launch our lanuchmodetest in this app testmainactivity see what the effect

(The code here is too simple), run the command after writing

You will find that Testmain is defined in the app Lanuchmode but if the external app launches him, he will still be in that external app task!

At the same time, in my simulator below 5.0 (4.3), you open the list of recently launched apps and you'll find this:

And when we get in, we'll find that we have to order a return button to get back to our outer app's own interface.

Singletop

This is a little bit more difficult, a little more complicated than the above. In fact, Singletop and STANDRD are basically the same, and can have multiple instances of activity,

The only difference is:

If the target activity of the call is at the top of the caller's task, the new instance is not created, but the current activity is used and the Onnewintent method is called.

The target activity is at the top of the caller's task stack, and many people can't understand it, but it is the translation that starts itself. For example, the search results interface in many apps

That's it, every time you search is actually called onnewintent. Imagine, if you don't use this singletop, then you search 100 times there will be 100 search pages.

Then you have to return to the last page 100 times.

Below to do an app, this app's testmainactivity page has a button click on him to start himself, we see what happens?

We can see that when the first launch of this testmain onnewintent is not walking is oncreate, after each send a

Start this page intent,oncreate are not going to go is onnewintent.

Now look at the situation in the task:

Of course, if you start from another app, because the other app has its own task, there can be 2 testmainactivity instances of course:

Singletask

This is a bit more complicated than singetop.

With this startup mode, the activity is only 1 in the entire system! Note that it is the whole system, not a task, which is the biggest difference from top.

If this instance exists, it will be accepted by Onnewintent to accept the intent. This is the same as top.

And will destroy all the activity in front of him.

1  <Activity2             Android:name=". Testmainactivity "3 Android:label= "@string/title_activity_test_main"4 Android:launchmode= "Singletask"5 Android:theme= "@style/apptheme.noactionbar">6             <Intent-filter>7                 <ActionAndroid:name= "Android.intent.action.TEST_LANUCHMODE" />8 9                 <categoryAndroid:name= "Android.intent.category.DEFAULT" />Ten             </Intent-filter> One         </Activity>

OK, let's explain this process in the form of graphs:

Let's assume that main clicks to jump to Testmain

Testmain Click to jump to Main2

Main2 Click to jump to Main3

The task condition at this time is:

At this point main3 click to jump to Testmain after the task situation is:

It fits our expectations, right, but if you change it a little bit:

1  <Activity2             Android:name=". Testmainactivity "3 Android:label= "@string/title_activity_test_main"4 Android:launchmode= "Singletask"5 android:taskaffinity=""6 Android:theme= "@style/apptheme.noactionbar">7             <Intent-filter>8                 <ActionAndroid:name= "Android.intent.action.TEST_LANUCHMODE" />9 Ten                 <categoryAndroid:name= "Android.intent.category.DEFAULT" /> One             </Intent-filter> A         </Activity>

You see, we've added a taskaffinity attribute, and this time we'll repeat the first step of the process, and you'll see that the task situation is this:

You see, here we find that although all of the activity in an app starts with each other, when you add this property, we jump from main to Testmain.

is a newly opened stack! Instead of the original main own that stack! When we jump from main3 to Testmain:

This is still the case! So pay attention to the effect of this property on Singtask!

Including your open Task Manager is this:

Just now we are talking about the Singtask in the application of the situation between the application of the external jump!

Note When jumping across apps, the Taskaffinity property does not affect Singetask. The same is true (students who are interested can try it themselves)

When jumping across apps, the situation is as follows:

1. When the target app is not in the process:

You see, at this time we are still active activity is the system of Lanucher and our own outerlanuchmode.

At this point, if you start lanuchmode the Testmain in this app, you will:

You see, this is how the system task is,

So the conclusion is that when the app that the target activity belongs to does not start, and the activity attribute belongs to Singetask,

The result of the caller launching this acitivity is that it must be in another task!

2. The second case:

The app that the target activity belongs to has been started, but the target activity hasn't started yet

At this point, if you click Start target activity

The result is also clear, of course, there is another complex situation, if the target activity already has the testmainactivity existence will be what, here does not write,

Interested students can write a demo to see the task of the situation.

SingleInstance

Finally look at this singleton mode, in fact, this is the simplest, that is, whenever the activity of this property is to occupy a stack alone.

and similar to the previous singletask, there is only one copy in the system!

Let's say we jump from main to testmain this single example:

1   <Activity2             Android:name=". Testmainactivity "3 Android:label= "@string/title_activity_test_main"4 Android:launchmode= "SingleInstance"5 Android:theme= "@style/apptheme.noactionbar">6             <Intent-filter>7                 <ActionAndroid:name= "Android.intent.action.TEST_LANUCHMODE" />8 9                 <categoryAndroid:name= "Android.intent.category.DEFAULT" />Ten             </Intent-filter> One         </Activity>

It looks like singletask, but if you open the Task Manager you will find:

Only one!

And when you get to this interface from the Task Manager, you can't go back to main when you click Back!

So while this is a 2 task, the system only displays one in the Task Manager!

and return to the previous acitivity, such a bad experience must be used with caution!

Of course the solution also has, just add taskaffinity attribute can!

Plus when you open the Task Manager again:

Everything is fine.

But still want to say a single example mode as far as possible with caution, especially without the taskaffinity attribute, according to the source I have seen at present should only

Some lanucher in the phone ROM will use this attribute, and I have never seen this attribute in any other app. So we must also use caution!

Because it will cause confusion to users!

Android graphics teach you to fully understand the activity startup mode

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.