Go Conflict between the loading mode of Android activity and the Onactivityresult method

Source: Internet
Author: User

Preface

Today, when debugging a program, it is found that clicking on a return key on an activity invokes the activity's Onactivityresult () method. I started with log, and later with a breakpoint tracking debugging for half a day, or the solution is baffled. Because the other activity before the Launchmode are normal, no special settings, the activity due to the need to change to singletop. It was not until I was sure that there was no place for the code to trigger, I thought about whether the activity's launchmode.

Explore

Searching for Android activity on Google Onactivityresult Singtop found some problems.

StackOverflow

Some people on the StackOverflow are similar to the problems I encountered. For example, if a developer sets the activity to Singletask mode, Onactivityresult will not receive any results. When he changed the Singletask mode back to Standard mode, he returned to normal.

One of the answers given below in this question is a saying in Startactivityforresult's document:

For example, if the activity is launching uses the Singletask launch mode, it won't run in your task and thus you Would immediately receive a cancel result.

This means: For example, if the activity you are loading uses the Singletask load mode, it will not run in your stack, and you will receive a canceled result immediately.

Immediately get a result_cancel in the Onactivityresult.

He added that there was no good remedy. Try listening to the broadcast method.

In another stackoverflow question, someone answered directly that the Startactivityforresult () method could not be used in singleinstance or singletop mode, and not only was adopted, but the number of votes was not low.

One of the remaining StackOverflow problems was that the change of Singletask to Singletop would be normal, with up to 59 votes to be adopted. I actually used singtop, but Onactivityresult was called for no reason.

documentation for Startactivityforresult:

public void Startactivityforresult (Intent Intent, int requestcode, Bundle options)

Added in API level 16

Launch an activity for which your would like a result when it finished. When this activity exits, your Onactivityresult () method would be called with the given Requestcode. Using a negative requestcode is the same as calling StartActivity (Intent) (the activity was not launched as a sub-activity) .

Load an activity, and when it is finished you will get the result. When this activty exits, your Onactivityresult () method is invoked according to the Requestcode given. Use a negative Requestcode and call StartActivity (intent) (activity is not loaded as a child activity)

Note that this method is should only being used with Intent protocols that is defined to return a result. In other protocols (such as Action_main or Action_view), you may not get the result when you expect. For example, if the activity is launching uses the Singletask launch mode, it won't run in your task and thus you Would immediately receive a cancel result.

Note that this method can only be used for intent protocols that are defined to return results. In other protocols (such as Action_main or Action_view), you may not get the results you want. For example, when you are loading the activity using the Singletask load mode, it will not run in your stack, so you will immediately get a cancellation result.

As a special case, if Startactivityforresult () with a requestcode >= 0 during the initial onCreate (Bundle save dinstancestate)/onresume () of your activity, then your window would not be displayed until a result was returned back from T He started activity. This was to avoid the visible flickering when the redirecting to another activity.

One exception is when you call Startactivityforresult () with a request code greater than or equal to 0 in the initial oncreate () method or the Onresume () method, and your window will not be displayed until the activity that is started returns results. This is to avoid blinking that is visible when jumping to another activity.

This method throws activitynotfoundexception if there is no Activity found to run the given Intent.

If the activity that runs the given intent is not found, the method throws a Activitynotfoundexception exception.

"Turn from" http://freetymekiyan.1kapp.com/android-activity%E7%9A%84%E5%8A%A0%E8%BD%BD%E6%A8%A1%E5%BC%8F%E5%92% 8conactivityresult%e6%96%b9%e6%b3%95%e4%b9%8b%e9%97%b4%e7%9a%84%e5%86%b2%e7%aa%81/

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.