Service start ActvityService start Actvity

Source: Internet
Author: User

Many times, we want a service to open a window when a condition is met. In Android, Actvity is started with a Service.
If you start Actvity on the Service, the key is to add a line of code
[Java] view plaincopyprint?
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Here we need to explain the significance of Flag.
(The following documents are taken from the Internet)
First, let's briefly describe the relationship between the Task and the Activity.
Task is like a container, and Activity is equivalent to filling in the container. The first thing (Activity) will be at the bottom, and the last added thing (Activity) it will be at the top. Retrieving an Activity from a Task is taken from the top, that is, the last added Activity is retrieved first, and so on. The last added Activity is retrieved, the sequence of the Activity in the Task can be controlled. When the Activity jumps, the Intent Flag can be used to set the creation method of the new activity;
Intent. FLAG_ACTIVITY_NEW_TASK www.2cto.com
The default jump type will re-create A new Activity, but in this case, for example, if Task 1 has three activities A, B, and C, start D in C, if. if the Affinity value added to D in the xml file is different from that in the Task, the Activity will be added to the newly marked Affinity Task. If it is the default or the specified Affinity is the same as the Task, a new Activity is started in the same standard mode.
FLAG_ACTIVITY_SINGLE_TOP
This FLAG is equivalent to singletop in the loading mode. For example, in the original stack, A, B, C, and D start D in D, and in the stack, A, B, C, D.
FLAG_ACTIVITY_CLEAR_TOP
This FLAG is equivalent to SingleTask in loading mode. The Activity started by this FLAG will bring up all the activities on the Activity to be started to the stack space. Class: In the original stack, A, B, C, and D are switched from D to B. At this time, the stack is A and B.
FLAG_ACTIVITY_BROUGHT_TO_FRONT
Many people write this online. If an activity exists in a task, it gets to the top and does not start a new Activity. This may mislead you! This FLAG actually means this! For example, if I have A and start B in A, add this flag to Intent in. In this case, B is started with FLAG_ACTIVITY_BROUGHT_TO_FRONT. Then C and D (normally C and D) are started in B. If B is started in D at this time, at this time, the final stack is A, C, D, B. note that the FLAG described by people on the internet above will be easily misunderstood as follows: A, B, C, and D are all standard loaded, then I start A in D. The intent and FLAG_ACTIVITY_BROUGHT_TO_FRONT will be mistaken for B, C, D, !! Actually, no. At this time, it should be A, B, C, D, A. Anyone who doesn't believe it should try it. However, the following mark and the mark will be clear to everyone!
FLAG_ACTIVITY_REORDER_TO_FRONT
In Intent. FLAG_ACTIVITY_BROUGHT_TO_FRONT: If A, B, C, and D are started normally, whether B is started with FLAG_ACTIVITY_BROUGHT_TO_FRONT or not, B is started in D, it will still become A, C, d, B.
 
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
This does not know how to use it. Reading the document is a bit like opening up a new Task, but it is not as simple as testing. Because the test results are very disappointing, The TaskId of the Activity started with this is the same! How to use it! Please leave a message. Thank you!
 
FLAG_ACTIVITY_NO_USER_ACTION
OnUserLeaveHint () is part of the activity cycle. It is used when the activity is returned to the background because the user wants to jump to another activity.
For example, when you press the Home Key (your choice), it will be called. For example, a call (not a user's choice) will not be called.
Then, how does the system distinguish the current activity from the background using the user's choice?
It is determined based on whether FLAG_ACTIVITY_NO_USER_ACTION exists in the Intent of the newly started activity that prompts the current Activity to return to the background.
Note: This function is not called when the activity is destroyed by calling finish ().
 
FLAG_ACTIVITY_NO_HISTORY
Mark it as your name suggests! That is to say, the Activity started with this FLAG will not exist in the stack once launched, for example! It turns out to be A, B, C. At this time, C starts D with this FLAG, and D starts E again. At this time, the stack is A, B, C, E.

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.