-2

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.

Stefano Cazzola
  • 1,597
  • 1
  • 20
  • 36
  • 3
    I suspect you want to read requestCode inside the Activity that was started using `startActivityForResult()`. Pass your request code as an extra in your Intent as well before calling `startActivityForResult()` and then read it in `onCreate()` using `getIntent().getExtras();`. There is no other way. – M-Wajeeh Oct 07 '13 at 08:57
  • 1
    I suspected. But, honestly, this is stupid. Thank you for the reply. – Stefano Cazzola Oct 07 '13 at 09:17
  • The request code is only for your own purpose, in the calling Activity. That way you know what Activity finished in the onActivityResult method. – nhaarman Oct 07 '13 at 09:35
  • Request code is meant for calling Activity, you should not use it. Instead you should pass something more intuitive of your own like Uri etc. – M-Wajeeh Oct 07 '13 at 09:57

2 Answers2

2

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.

ann
  • 576
  • 1
  • 10
  • 19
Garytech
  • 328
  • 3
  • 6
0

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.

Aman Gautam
  • 3,549
  • 2
  • 21
  • 25