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?
-
check out this question: http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity – slayton Oct 17 '11 at 20:01
-
Im not getting what this has to do with my question – coder_For_Life22 Oct 17 '11 at 20:02
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?
-
-
So there is no way to get the Activities bundle outside of onSavedInstanceState, onCreate and onRestoreInstanceState? – coder_For_Life22 Oct 17 '11 at 20:13
-
I think ill just be better off using SharedPreferences other than making this more complicated than it should be. 1+ and answer for your time and effort. – coder_For_Life22 Oct 17 '11 at 20:18
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.

- 22,171
- 3
- 46
- 43
-
Thanks alot. Your last part was very benficial to what i am trying to do – coder_For_Life22 Oct 17 '11 at 20:36