Class variables set to onActivityResult are reset when the method returns

I have an activity that allows the user to select a phone number. Naturally, I would like my class to remember the identifier of the selected contact, so I save it in the class field. However, when the onActivityResult method returns, my class variable is reset. Here is what I am 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); switch(reqCode){ case(PICK_CONTACT): if(resultCode == Activity.RESULT_OK){ Uri contactData = intent.getData(); Cursor c = managedQuery(contactData, null, null, null, null); if(c.moveToFirst()){ contactName = c.getString(c.getColumnIndexOrThrow(People.NAME)); contactId = c.getInt(c.getColumnIndexOrThrow(People._ID)); break; 

When I set a breakpoint in this method, the values ​​for contactName and contactId will be what I expect, however as soon as the method returns, the values ​​will somehow get reset to default. It's clear that I'm missing something, but I'm not sure what I'm doing wrong or forgetting.

Thanks!

Iva

+4
source share
1 answer

Not sure if you still need help. onActivityResult is called before onResume when your activity restarts. Make sure that you do not reset the values ​​of the variables in onResume.

You will receive this call immediately before onResume () when your activity restarts.

http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int , int, android.content.Intent)

0
source

All Articles