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?
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With