Android activity using Supplements

Source: Internet
Author: User

First, onwindowfocuschanged

Sometimes we need to measure how long it takes for an activity to show up, so where is the time to tick the code? After the execution of OnCreate and Onresume, the activity interface is still not visible, and after Onresume, the framework will callback a function called onwindowfocuschanged. It indicates whether the user is already able to interact with the activity's interface. Onwindowfocuschanged to true means that the activity interface is already visible to the user (and can naturally interact with the user). In fact, the point at which activity becomes visible appears before onwindowfocuschanged, but this state can only be obtained in Activitymanagerservice, A flag that is visible or not visible on the client or only through onwindowfocuschanged as an interface.

When the interface becomes invisible, call OnPause first, and then onwindowfocuschanged to false. This is also easy to understand, OnPause is originally the activity is partially covered when the call, after the call OnPause will not allow the interface to continue to interact with the user.

If you want to speed up the activity startup time and move some of the time-consuming operations in the OnCreate to the Onresume, you may find that there are no changes. So if you want the activity interface to be displayed to the user as soon as possible, you have to put the logic on onwindowfocuschanged to true after the execution.


Second, Onuserleavehint

This callback function is mainly used to listen to the action of pressing the home key to exit to the desktop, which occurs before onpause. When a new activity is started, Activitystacksupervisor will call startactivityuncheckedlocked, which will be assigned to muserleaving. Muserleaving is used to indicate whether the function onuserleaving is called when the current activity is retired to the background.

Visible, only if the Flag_activity_no_user_action flag is set muserleaving will be false, in other cases muserleaving is true, that is, Onuserleavehint will be called, The note also says that Onuserleavehint will be called before OnPause.

Third, taskaffinity

A task is a set of activity that is used to process a task, in a fallback stack, and when a new application is launched, the framework creates a new task to host the corresponding activity. The taskaffinity is used to describe the affinity of the activity, which task the activity belongs to. If taskaffinity is not explicitly defined in Androidmanifest.xml's application, then the default affinity name is the package name. The activity of the same affinity is placed in the same task, the affinity name of the task is determined by the taskaffinity of the root activity, if the root activity is not set taskaffinity attribute, Then the affinity was decided by application's affinity.

if Taskaffinity is set to an empty string, it means that the activity and other tasks do not have affinity. Activity for different applications can also set the same affinity so that they will be in the same task when they are started.  

Iv. Launchmode

1. Standard

The default Launchmode, which can be instantiated multiple times.

2. Singletop

The use of this launchmode is that if the activity to be started is already at the top of the stack, then startactivity will no longer call its oncreate again, but instead call its onnewintent. However, if the activity to start is not on the top of the stack, such as a–> bàc, then C and then start B, it will instantiate a new B, the stack into aàbàcàb.

If a is clicked on a button to start B, but the system is slow, the user points in a very short time two times, if the B is standard, then there will be two B, if the singletop will only have one. A more typical scenario is when a user pays an order using the takeaway app, returns to the Order Details page from the payment page, and if the status of the order changes, such as a merchant receipt, the notification bar will be notified, and the notification will jump into the Order Details page. If the Order Details page is standard, then the user press the back key after the discovery or on the Order Details page, the experience is very poor. If it is singletop, just update the status information in the Onnewintent, you can display the most up-to-date information without having to create a new activity again.

3. Singletask

Activity that starts in this mode, if an instance of the activity already exists in the task, calls its Onnewintent method to enter the task. If there is no instance of the activity, a new task is created and the activity is the root activity of the new task. So activity that starts with Singletask does not necessarily create a new task. It is important to note that even if the activity is in a new task, pressing the back key will still return to the previous activity that started it, although they are not in the same task.

If the Singletask activity's taskaffinity is the same as the affinity of an existing task, then the activity is created directly in the existing task, So the activity that starts with singletask is not necessarily the root activity at the bottom of the stack.

4. SingleInstance

Activity initiated in this mode has its own exclusive task, and if it already has an instance of the activity, it will be onnewintent when it is started again. However, the activity initiated by SingleInstance is different from Singletask. For example a start b,b is singleinstance,b and then start C, then B itself is in a new task, and A and C are in a task, then when you press the back key, the C will not be returned to B, but instead go back to a, then press the back key to return to B.

Note that the intent flag can also be set in the code to control the activity's startup mode, and the code dynamically sets a higher priority than the static write Android:lauchmode in Androidmanifest.xml.  


V. The Flags of Intent

1. Flag_activity_new_task

Create a task for the activity, if the startactivity context is not an activity, but a service or application, be sure to add this flag. The act of activating activity with this flag is consistent with Launchmode for Singletask. But not every time a new task is created, the activity is put in it, but the first thing is to find out if there is already taskaffinity the same task, if there is to start the activity in the task, if there is no taskaffinity the same task, Before a new task is created.

2. Flag_activity_single_top

function with Singletop.

3. Flag_activity_clear_top

When you start an activity, if you already have an instance of the activity in your task, all activity on it is cleared. For example, the ACTIVITY initiation relationship in a task is aàbàcàd, and then when D starts B, with the FLAG_ACTIVITY_CLEAR_TOP flag, only A and B are in the task after the start of B, and C and D are cleared. If B's Launchmode is standard, then when it receives intent, it will pin down the instance of the original B and then re-oncreate to build a new B; if B is the Singletask type, then the original B instance will be preserved. Call its onnewintent, and pass in the new intent.

4. Flag_activity_clear_task

When you start the activity, all other activity in the task is cleared. For example, the ACTIVITY initiation relationship in a task is aàbàc, and when B starts C, plus flag_activity_clear_task, then after C starts, only C is left in the task, and A and B are cleared. If there is only one task in this application, pressing the back key in C will return to the desktop.

5. Flag_activity_reorder_to_front

Activity that starts with this flag is moved to the top of the stack if it already exists in the task, and the other activity order remains the same. If it does not exist in the task, create a new one. For example, the activity initiation relationship in a task is a->b->c->d, and when D starts B, when the flag is added, B is moved to the top of the stack, and the task becomes A-C-D - B. If B has been tuned to finish, but the system has not yet moved it out of the task, starting B in Flag_activity_reorder_to_front mode, B will not start. B can only be started after B destroy.

6. Flag_activity_no_history

This flag allows the activated activity to exit once it is removed and is no longer present in the stack. For example, the ACTIVITY's start-up relationship is a-> c->d, and when B starts C, plus flag_activity_no_history, after C starts D, the task is still the a-> b->c->d stack layout, C is not purged from the stack. When I press the back key in D, I do not return to C but fall back to B, when Dumpsysactivity will find that there is no C in the task, only A and B, when you press the back key in D, C will finish off.

7. Flag_activity_exclude_from_recents

Activity started with this flag will not appear in the list of recently launched apps.

8. Flag_activity_forward_result

We use Startactivityforresult and Setresult to pass data between two activity, if there is another activity in the middle, such as a-A and b->c, If you want to use Startactivityforresult and setresult between A and C, you need to add this parameter when B starts C. That is, a normal startactivityforresult start b,b start C in Flag_activity_forward_result mode, Setresult in C, so that when you return from C to B, and then back to A, C and B are finish, The onactivityresult of a will receive the value of Setresult in C.

9. Flag_activity_launched_from_history

This is set by the system when the activity is launched from the recent task list, which is not used by third-party apps, and is not set by the system's Systemui in the recent task list.

10. Flag_activity_no_animation

The entry animation will not be performed to start the activity.

11. Flag_activity_task_on_home

The task that is activated with this flag will be on the task where the home is located, that is, pressing the back key in the activity will return to home instead of the activity that initiated it. For example, taskaffinity A and b,b are different from A, a starts with B with both the Flag_activity_task_on_home and the Flag_activity_new_task, and A and B are in different tasks , if B is only started in NewTask mode, then even if B and a are not in a TASK, then pressing the back key in B will return to a, but with Flag_activity_task_on_home, pressing the back key will return to home. At this point A has been moved to the background.


        the original team of embedded Penguin Circle consists of senior engineers such as Ali, Meizu, nvidia, godson, Ju Li, and so on. Hundreds of original, two articles a week, sharing embedded, Linux, Internet of Things, GPU, Android, autonomous driving and other technologies. Welcome to sweep code attention to the public number: embedded Penguin ring, real-time push the original article!


Android activity using Supplements

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.