Some knowledge about task in Android

Source: Internet
Author: User

Task in Android is a logical concept, simply put, is a stack of multiple activity stored in order. The activity can come from the same app, and it can be from different apps.
Task is created by analogy to the previous Todo list demo, when it is installed on the phone, in the application list, also known as launcher, there will be an icon to display, assuming long press the icon, but also for it to create a shortcut on the desktop.
When you click on this icon, or shortcut, the main activity of the Todo list entry is created, and at the same time a stack is created, and then the activity is placed in the stack.
When main activity, click on the list item, Detail activity will be created, also put in the stack.
When you click "View image" in detail activity to enter image Activity,image activity is also placed in the stack,
When image activity, through intent, calls another app (camera) in the system to get the photo, it actually invokes an activity in the camera app, which, by default, is placed in the current stack.

The above process, for example, is seen in the following:


This sequence of activity sequences, the concept in Android is a task. In layman's words, it represents a feature display that is provided by an app order, including its ability to invoke other apps.
The end of the task and when the user taps the back key, or the return key on the Actionbar, the current activity is bounced out of the stack, then destroyed, so that it always returns, and the last activity, in the example above, is
Main Activty will also be ejected from the stack and destroyed, and the result is to go back to the desktop, when the last activity from the stack is removed, the stack is also destroyed, then a task will not have the meaning of existence.
Then there is no task at this time.
Task switch So when you start an app from Launcher or a shortcut, assuming that the app hasn't been started recently, a new stack is created and a new task is started.
Home app, also a task, when the user clicks the Home button, the screen will run to the desktop, that is, the current task is stopped, and switch to the task of home.
At this point, the task that originally appears on the screen is moved to the background, in the depths of the user's invisible screen, where all the activity in the task is in the stop state, but all the content and state is saved.
When you click the app's icon from Launcher, or a shortcut, the task in the background is taken to the screen, the user can continue, and the task where the home app resides is switched to the background.
So when the user looks at the screen, in the invisible virtual space behind the screen, there may be multiple tasks at the same time, silently, waiting to be brought to the screen at any time.

The processing described above occurs by default in Android. In this case, when an activity is created, an activity is placed into the stack, and when the previous activity is returned, the current activity is removed
And the task can have multiple same activity at the same time.

Of course, Android also provides some attributes or flags to allow users to change the default processing behavior, although the Android development team does not recommend this, because in most cases, the use of the default mechanism has been very good to meet the needs of the app.
Launch modelaunch mode is a property provided by Android on the,<activity> element in Androidmanifest and has the following four values:
1) Standard This is the default value for Launcher mode, which is the case above.
2) Singletopsingletop is to ensure that the same activity, only one instance at the top of the stack, for example, by default, assuming that the activity in the stack is a->b->c->d, Assuming that in D and using intent to open a D activity, then the stack will be this sequence, a->b->c->d->d.
Assume that the following properties are set in the activity of D,
Android:launchmode= "Singletop"
Then the intent will be received and processed by the current activity (i.e. D) 's Onnewintent method, then
The sequence in the stack is still the same as the original, i.e. A->b->c->d.
And in the actual example, there may be a situation, in the ListView high-speed click on the ListItem, because there is no processing DoubleClick event, or self-rewriting the Ontouch event, it is possible that these two clicks will trigger
The same ItemClick event, which results in the opening of two identical activity, such as A->b->b, then the return in B, the discovery or return to B, to return again is to go back to A, the effect will be very strange.
In this case, the Singletop attribute can be passed to avoid this error.
3) Singetask Assuming that the property set Launchmode in the activity is singletask, the activity will be created when it first checks if there is a task in which there is such an activity, if any, Then Android will directly
Intent is routed to that activity and is handled by its Onnewintent method, and if there is no such task, then Android creates a new stack and then puts the activity in as a stack.
There is a case where there is already a activty in a task, assuming that the activity has activity on it, that is, if it is not on top of the stack, then its activity will be destroyed.
And assuming that it has activity below, it and all the activity below it will be taken to the current task, that is, if you press the return key on the activity, the return is not the activity that opened the activity,

Instead, the following activity is in its original stack. Figure Demo Sample For example the following:


4) SingleInstance Such a case is the same as Singletask, and the difference is that the task it creates or is looking for can only have activity to start, and only one can.
The above four cases are set by the Launchmode property of the activity in the Androidmanifest file.
Intent flag can change the default task management flag by setting the Intent flag when the next activity is started by startactivity in the code, such as the following:
Intent Intent = new Intent (mainactivity.this, Detailactivity.class);
Intent.setflags (Intent.flag_activity_single_top);
The value of flag has the following three:
1) Flag_activity_new_task This value is the same as the effect of singletask in Launchmode.
2) Flag_activity_single_top This value is the same as the effect of singletop in Launchmode.
3) Flag_activity_clear_top

When intent sets such a flag, then Android will look for an existing activity, assuming that there are other activity on it, it will erase all of its activity.

End.

    

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.