Startactivityforresult unable to invoke Singletask activity

Source: Internet
Author: User
Tags home screen

Background:

Suppose an activity a, you need to use the Startactivityforresult method to open activity B. If the launch mode of B is set to Singletask/singletop, then in the instant after B is opened (without waiting for B to return any result), the Onactivityresult method in a is called and receives a Result_ Cancel's request code. Reason:The startactivityforresult of a can not be used if the open B and open B are different when the two activities work in a task. So, what is a task in Android? By default, activity from the same app exists in the same task. A stack is maintained in each task, which records the activity that was opened and not destroy. At the top of the stack is the current activity in foreground. When you open an app in Home screen, it's the equivalent of a new task being opened. Every time you press the back key, it is equivalent to removing the topmost activity of the task's stack. But the same task stack can also contain activity from different applications (for example, your own app can start another browser's page). When declaring an activity's launch mode as Singletask in Androidmanifest.xml, when the activity is created, it is placed in a new stack and placed on top (that is, placed in a new task). If the activity is already in the stack of a task, only its onnewintent () is called, and OnCreate () is not called. When declaring an activity's launch mode as singleinstance in Androidmanifest.xml, the same as the Singletask mode when creating this activity, It is placed in a new stack and placed on top (that is, in a new task). Unlike the Singletask model, the new task stack will always have only one element, which is the activity itself. If the activity is already in the stack of a task (the stack must have only one element, that is, the activity itself), only its onnewintent () will be called, and OnCreate () will not be called. Workaround:If your B does need to use the single task's launch Mode, and B does need to return to a some data, try to re-open a in B via StartActivity (Intent Intent). But at this point, you have added the data you want to return in the intent.

Startactivityforresult unable to invoke Singletask activity

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.