Android onActivityResult is invalid or is executed first or has no callback problem. androidactivity uploads the value

Source: Internet
Author: User

Android onActivityResult is invalid or is executed first or has no callback problem. androidactivity uploads the value

My problem is the first case. After the change, the result is returned.

Http://www.cnblogs.com/tt_mc/p/3586834.html)

Conflicts between the loading mode of Android Activity and the onActivityResult Method

Preface

During program debugging today, it is found that clicking the return key on an Activity will call the onActivityResult () method of the Activity. I started to use log, and then used breakpoint tracking and debugging for half a day. I was still puzzled. Because the LaunchMode of other previous activities is Normal and there is no special setting, this Activity is changed to singleTop as needed. It wasn't until I confirmed that no part of the code was triggered manually that I thought about whether it was related to the LaunchMode of the Activity.

Exploration

Search for android activity onactivityresult singTop on Google and find some problems.

Stackoverflow

Some people on stackoverflow have similar problems with me. For example, if a developer sets the Activity to the singleTask mode, onActivityResult will not receive any results. When he switches the singleTask mode back to the standard mode, it returns to normal.

In the answer below, one of the documents that says startActivityForResult contains the following sentence:

For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.

For example, if the activity you are loading uses the singleTask loading mode, it will not run in your stack, and you will immediately receive a cancellation result.

In onActivityResult, A RESULT_CANCEL is obtained immediately.

He also added that there is no good remedy. You can try the method of listening to broadcasts.

In another stackoverflow question, someone directly answered the question that the startActivityForResult () method cannot be used in singleInstance or singleTop mode, and the number of votes is not low.

In one of the remaining stackoverflow problems, it was said that it would be normal to change singleTask to singleTop, And the ticket was received as high as 59 votes. In fact, I am using singTop, but onActivityResult is still called for no reason.

StartActivityForResult document:

Public void startActivityForResult (Intent intent, int requestCode, Bundle options)

Added in API level 16

Launch an activity for which you wowould like a result when it finished. when this activity exits, your onActivityResult () method will be called with the given requestCode. using a negative requestCode is the same as calling startActivity (Intent) (the activity is not launched as a sub-activity ).

Load an Activity and you will get the result when it ends. When this Activty exits, your onActivityResult () method will be called Based on the given requestCode. Using a negative requestCode is the same as calling startActivity (intent) (activity is not loaded as a subactivity)

Note that this method shoshould only be used with Intent protocols that are defined to return a result. in other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you have CT. for example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.

Note that this method can only be used to define the Intent protocol for returning results. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result if you want it. For example, when the singleTask Loading Mode of the Activity you are loading is used, it will not run in your stack, so that you will immediately get a cancellation result.

As a special case, if you call startActivityForResult () with a requestCode> = 0 during the initial onCreate (Bundle savedInstanceState)/onResume () of your activity, then your window will not be displayed until a result is returned back from the started activity. this is to avoid visible flickering when redirecting to another activity.

One exception is that when you call startActivityForResult () with a request code greater than or equal to 0 in the initial onCreate () method or onResume () method (), your window is not displayed until the started Activity returns results. This is to avoid blinking visible when you jump to another Activity.

This method throws ActivityNotFoundException if there was no Activity found to run the given Intent.

If the Intent Activity is not found, the ActivityNotFoundException exception is thrown.

Activity Loading Mode
Use Cases Launch Mode Multiple Instances? Comments
Normal launches for most activities "standard" Yes Default. The system always creates a new instance of the activity in the target task and routes the intent to it.
"singleTop" Conditionally If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to itsonNewIntent()Method, rather than creating a new instance of the activity.
Specialized launches
(Not recommended for general use)
"singleTask" No The system creates the activity at the root of a new task and routes the intent to it. however, if an instance of the activity already exists, the system routes the intent to existing instance through a call to itsonNewIntent()Method, rather than creating a new one.
"singleInstance" No Same"singleTask", Could t that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

SingleTop mode can be used to solve the problem of multiple identical activities at the top of the stack.

The singleTask mode and the singleInstance mode are used to create only one instance.

When intent arrives and a singleTask Activity needs to be created, the system checks whether there are instances of the Activity in the stack. If any, send intent directly to it.

The singleInstance mode solves this problem (the question is answered after such a long time ). Let the Activity in this mode be independent in a task stack. This stack has only one Activity. The intent sent by the tour guide application and google map application is received and displayed by this Activity.

Summary

Later, I changed the specific implementation method of refreshing the interface when the ResultCode in onActivityResult is RESULT_ OK. However, onActivityResult will still be called by myself, but it does not trigger any bugs yet, but it is still a time bomb.

In the future, when selecting the loading mode of the Activity, the onActivtyResult method should conflict with it.

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.