问题
can I access the requestCode an activity received in its onCreate() method?
I'd need it to understand if it is a "new" or "modify" operation.
回答1:
Let's say you create an activity A :
The activity lifecycle goes like this : A.onCreate() -> A.onStart() -> A.onResume()
Then you call activity B from activity A. You want activity B to send you back a result.
The activity lifecycle goes like this : A.onPause -> B.onCreate ->B.onStart()-> B.onResume()->A.onStop()
Once B has done its job, it will send a result and destroyed itself : B.onDestroy()->A.onResume()-> A.onActivityResultBack()
My whole point is that the activity A does not go back to onCreate
! So unless you have an attribute requestCode in your activityA.java file, you can not access it in its onCreate
method.
回答2:
To see if it's a "new" or "modified" activity, you should override the onActivityResult method in the activity. If the onActivityResult is getting called, that means the activity is "modified".
See http://developer.android.com/training/basics/intents/result.html for how to use the onActivityResult.
来源:https://stackoverflow.com/questions/19219926/android-request-code-in-oncreate