Search code examples
androidonactivityresultstart-activity

StartActivityForResult() and OnActivityResult() both needed?


Is it necessary to start an Activity with startActivityForResult() to set a result in second activity or,

one may start the Activity by normal startActivity() and still set a result in second Activity and get it back using onActivityResult()?


Solution

  • The method Activity.onActivityResult is a callback. It is different from startActivity and startActivityForResult because the response is asynchronous.

    You can set the result of an Activity without requirement, but the method onActivityResult will only be called if the activity that 'resulted' was started with startActivityForResult.

    So yes, if you want to start an activity and obtain a result, you need to first start it using Activity.startActivityForResult, then override onActivityResult and catching the result there.