I. Problem description

StartActivityForResult is one of the most common ways to get data back from other pages in development, and it’s hard to believe that it crashed on one of the pages of a recent project. Print the onActivityResult () method, resultCode says RESULT_CANCELED because the code says RESULT_OK. I checked the code and found nothing wrong with the syntax, leaving the startActivityForResult () and setResult() methods in the source code.

Second, analyze the source code

Take a look at the startActivityForResult () source code, and the annotation for this method seems to find something:

    * <p>Note that this method should only be used with Intent protocols
    * that are defined to return a result.  In other protocols (such as
    * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
    * not get the result when you expect.  For example, if the activity you
    * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
    * run in your task and thus you will immediately receive a cancel result.
Copy the code

Activities on different stacks retrieve data in the form startActivityForResult and return cancel. The launchMode of the Activity is set to “SingleTask”.

Third, summary

Activities of different stacks obtain data in the form of startActivityForResult, which will directly return the result of Cancel. If the page needs to return result, carefully set the launchMode.