Class variables set in onActivityResult are reset when method returns

I have an activity which lets the user select a phone number. Naturally, I'd like my class to remember the id of the contact selected, so I save this in a class field. However when the method onActivityResult returns, my class variable is reset. Here is what I'm trying to do:

    Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT); 


    public void onActivityResult(int reqCode, int resultCode, Intent intent){
        super.onActivityResult(reqCode, resultCode, intent);       
                if(resultCode == Activity.RESULT_OK){
                    Uri contactData = intent.getData();
                    Cursor c = managedQuery(contactData, null, null, null, null);
                        contactName = c.getString(c.getColumnIndexOrThrow(People.NAME));
                        contactId = c.getInt(c.getColumnIndexOrThrow(People._ID));


When I set a breakpoint within this method, the values for contactName and contactId are as I expect, however once the method returns, the values somehow get reset to their defaults. Clearly I'm missing something, but I'm not sure what I'm doing wrong or forgetting.



Asked by: Melissa428 | Posted: 20-01-2022

Answer 1

It's Possible that your Activity is being Suspended and Re-created.

Essentially, if the Intent you're launching with startActivityForResult is resource-intensive enough, the OS might have to suspend your Activity to free up resources.

So it saves what it can and then returning from the Intent to call onActivityResult, it has to re-create your Activity. Any instance variables will be reset in this case.

You get around this by either restarting the device or handling it with onSaveInstanceState and onRestoreInstanceState.

Read more here: Why oncreate method called after startActivityForResult?

Answered by: Kelvin523 | Posted: 21-02-2022

Answer 2

Not sure if you still need help with this. onActivityResult is called before onResume when your activity is restarting. Make sure you are not resetting the variable values in onResume.

You will receive this call immediately before onResume() when your activity is re-starting., int, android.content.Intent)

Answered by: Chelsea207 | Posted: 21-02-2022

Answer 3

May be your configuration is changing when you open new Activity. Try to put configChanges on your activity in AndroidManifest.


Answered by: Grace844 | Posted: 21-02-2022

Similar questions

android - Alert shown in onActivityResult

I am trying to show custom alert from onActivityResult. I need to inflate it, so am getting context with getApplicationContext() and everything is fine until I execute - then it fails with: Unable to add window -- token null is not for an application Do you know why I cannot do it? (it happens on 1.6 and 2.0 - I didn't test others)...

android onActivityResult not calling

I'm beginner in Android. I have two screens. I am switching between the screens, but onActivityResult() is not called in my application. I'm getting my main screen back. I want result of my second screen. This is my code: loadPicture.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { Intent i = new Inten...

android - Can I get original request data (intent extras) in onActivityResult callback?

Curious, if I can invoke some 3rd party activity, and then in onActivityResult read my original intent data.

android - onActivityResult no longer being called after onDestroy

onActivityResult is giving me major headaches. My little game has a title / menu screen. Upon clicking "new game" an Activity is launched which creates a SurfaceView for my game world. When playing, the player can enter buildings in the game world. Upon entering a building I launch the activity for that particular building from my SurfaveView. Here is an example: Intent storeIntent = new Inten...

Android 1.6: onActivityResult is not called

I have a problem with the method "onActivityResult". I create a new Activity from my main activity: Intent intent = new Intent(this, NewActivity.class); startActivityForResult(intent, 0); The new Activity is ended like this: Intent resultIntent = new Intent(); resultIntent.putExtra("vid", hmvenues.get(venues[currentPosition])); resultIntent.putExtra("name", venues[currentPo...

android - Not getting desired value in "onActivityResult"

Warning **07-07 15:24:25.021: WARN/ActivityManager(57): Activity is launching as a new task, so cancelling activity result.** 07-07 15:24:25.681: WARN/ActivityManager(57): Activity pause timeout for HistoryRecord{43cccce8 com.tcs.QuickNotes/.QuickNotesHome} 07-07 15:24:27.392: INFO/AudioPathIn onActivityResult(1370): onActivityResult 07-07 15:24:27.410: INFO/requestCode(1370): requestCode1 07-07 15:24:27.4...

email - Get Mail Sent Notification in onActivityResult "Android"

I am launching a mail activity by //Sending mail final int SENT_MAIL = 1; startActivityForResult(Intent.createChooser(i, "Send mail"),SENT_MAIL); and in onActivityResult(int req, int res,Intent data) i am trying to find the result of email sending, so as to confirm if my mail was sent or was discarded by the user. I am...

onActivityResult fc when using SurfaceView on Android

My app fc when activity that's using SurfaceView as content view calls startActivityForResult(...) and activity that has been started calls finish() This does not happen if I change content view to something else than SurfaceView. This would be the requested logcat: 09-05 00:17:18.926: ERROR/AndroidRuntime(339): Uncaught handler: thread main exiting due to uncaught exception 09-05 00:17:18.937: ERRO...

android - how to get tow intent result in onActivityResult?

I am using two Intents for various actions in my activity and I also used onActivityResult to get a result. I want to know how to get to Intent result in onActivityResult? Can someone please provide sample code? Thanks all.

android - After onActivityResult event State Is Gone (Droid Phones)

I have a problem that's only affecting Droid phones. I end up not being able to access global variables after an onActivityResult event. While looking into this I came across: How to declare global variables in Android? I follow those directions, but still the value is NULL. On my HTC Hero everything I'...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android