Common flags for Android intent

Source: Internet
Author: User

First, our common code that jumps from mainactivity to another main1acticity:

1 Intent mintent=new Intent (mainactivity.  this, main1activity. class ); 2 startactivity (mintent);

In general, the above two activity instances are placed in the same stack as the task. However, by understanding the stack principle of task, we know that each application has its own task flag taskaffinity, different instances of activity can be run in different stacks, the following is a few flags that are common in intent.

1.flag_activity_new_task: When the intent object contains this tag, the system will look for or create a new TASK to place the target ACTIVITY, The search is matched against the taskaffinity attribute of the target activity, and if the taskaffinity of a task is found to be the same, the target activity is pressed into the task, and if the lookup is fruitless, a new task is created, The taskaffinity of the task is set to the taskactivity of the target activity, and the target activity is placed on this task. Note that if the taskaffinity of the activity in the same app uses the default or both sets the same value, it doesn't make sense to jump between activity in the app using this tag because the current application task is the best host for the target activity.

1 Intent mintent=new Intent (mainactivity.  this, main1activity. class ); 2 mintent.setflags (intent.flag_activity_new_task); 3 startactivity (mintent);

      2.flag_activity_ Clear_top: When the intent object contains this tag, if an activity instance is found on the stack, the activity above the instance is emptied to the top of the stack. For example: Our firstactivity jumps to secondactivity,secondactivity jump to thirdactivity, and thirdactivity jumps to secondactivity, Then the thirdactivity instance will be ejected from the stack, secondactivity at the top of the stack, shown before the screen, only firstactivity and secondactivity are left in the stack. This secondactivity can either receive the intent in onnewintent (), or it can be destroyed and restarted to accept the intent. In the default "Android:launchmode=" Standard "startup mode, if you do not use the flag_activity_single_top tag in intent, then it will turn off after rebuilding, if you use this Flag_ Activity_single_top tag, the existing instance is used, and for other startup modes No more flag_activity_single_top is used, it will use an existing instance. Intent will be passed to the onnewintent () of this instance.

1 Intent mintent=new Intent (mainactivity.  this, main1activity. class ); 2 mintent.setflags (intent.flag_activity_clear_top); 3 startactivity (mintent);

When using the default "Android:launchmode=" Standard "startup mode, the activity does not have to close the rebuild

1 Intent mintent=new Intent (mainactivity.  this, main1activity. class ); 2 mintent.setflags (intent.flag_activity_clear_top|  Intent.flag_activity_single_top); 3 startactivity (mintent);

3.flag_activity_single_top: When a target ACTIVITY instance exists in a task and is at the top of the stack, no new one is created and the instance is directly exploited.

4.flag_activity_clear_when_task_reset: If a intent contains this attribute, The activity that it turns to and all the activity on that activity will be purged from the task when the task is reset. When we return a back-end task to the foreground, the system will have a flag_activity_reset_task_if_needed tag attached to the action in a particular case, which means that the task is reset if necessary, flag_activity_clear _when_task_reset will take effect. After testing, for a background application, if the main menu click on the application, this action contains flag_activity_reset_task_if_needed tag, long press the home key, and then click the recent record, this action does not contain flag_activity _reset_task_if_needed tags, so the former will be cleared, the latter will not.

1 Intent mintent=new Intent (mainactivity.  this, main1activity. class ); 2 mintent.setflags (intent.flag_activity_clear_when_task_reset); 3  StartActivity (mintent);

5.flag_activity_reset_task_if_needed : This tag will take effect in the following cases: 1. Create a new task to place the activity instance when the activity is started; 2. The existing task is placed in the foreground. The specified task is reset based on affinity, and the task presses into some activity instance or removes some activity instances. We combine the above clear_when_task_reset to deepen our understanding.

1 Intent mintent=new Intent (mainactivity.  this, main1activity. class ); 2 mintent.setflags (intent.flag_activity_clear_when_task_reset|  intent.flag_activity_reset_task_if_needed); 3 startactivity (mintent);

Common flags for Android intent

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.