Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull string from bundle in onResume()?

Tags:

android

I have an activity that is resumed after a user picks a contact. Now before the user picks a contact onSavedInstanceState is called and i put a string in the Bundle. Now, After the user selects the contact and the results are returned. onRestoreInstanceState doesnt get called. only onResume() gets called. So how would i go about pulling my string back out of the bundle once the activity is resumed?

like image 729
coder_For_Life22 Avatar asked Oct 17 '11 19:10

coder_For_Life22


2 Answers

Lets say you have two Activities A and B, and Activity A starts Activity B. If you want to pass information from A to B, you can pass information from A to B with:

Intent i = new Intent(this. ActivityB.class);
i.putExtra("Key","Value");
startActivity(i);

Then in Activity B you can get the string with

String value = this.getIntent().getExtras().getString("keyName");

However, if you want to pass information from B to A you have to use a different method. Instead of using startActivity you need to use startActivityForResult. A description of this method is found here: How to return a result (startActivityForResult) from a TabHost Activity?

like image 111
slayton Avatar answered Sep 22 '22 06:09

slayton


First, why onRestoreInstanceState isn't firing: According to the documentation, onRestoreInstanceState gets called after onStart(), which, according to the activity lifecycle diagram, only gets called after onCreate or onRestart. If your main activity isn't destroyed when the user goes to choose a contact, then onStart will never fire, and onRestoreInstanceState will never fire. The diagram shows this to be the case when "Another activity comes in front of the activity", and onPause is fired - From there your Activity will only be killed if the system needs more memory.

Second, how to get the data you saved before choosing a contact- A local variable should do it, since the activity is staying in memory. If you get to a point where the activity does not stay in memory, onRestoreInstanceState should fire.

like image 25
Alexander Lucas Avatar answered Sep 19 '22 06:09

Alexander Lucas