Recognizing multiple intents (for onActivityResult

2019-07-13 23:28发布

问题:

I have 5 intents in one activity. All of them are using the startActivityForResult. Because all of them are to correspond to different elements in the layout. How do I recognize which intent is the result for.

For e.g. If I have intent1, intent2, intent3 all of which are using startActivityForResult. After the Intent business is done. How do I now in my onActivityResult recognize which intent was called. The intent being called upon is the android phone contacts intent.

回答1:

When you call startActivityForResult(), you set the requestCode. Later, you can use this request code inonActivityResult() to determine the intent. see What is Intent from onActivityResult Parameters for more. If you see the documentation of onActivityResult, it mentions:

protected void onActivityResult (int requestCode, int resultCode, Intent data)

requestCode: The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.