I have two Activities A1 and A2 , on firing some event i am calling A2 (through intent) from A1. Now inside A2 i am firing some event and based on that i am calling A1 again and passing data through intent).
Now the problem is When A1 gets called from A2 with data , A1 activity load with itself with a new state but i want to maintain its old state when A1 was first loaded. indirectly i don't want to call onCreate.
so far i have tried following code in A1 activity , its a static method in A1 which load itself
publ开发者_C百科ic static void show(Context context , int index)
{
final Intent intent = new Intent();
intent.setAction(MC_MY_ACTION);
intent.putExtra("routeIndex", index);
context.startActivity(intent);
}
from A2 i am calling A1 as follows from onOptionsItemSelected and passing the selectedMenuIndex
A1.show(this,selectedMenuIndex);
If you want to save the state of your activity and have it persist through multiple onCreate() calls, you can use the Bundle object. You can save to the Activity's Bundle in onSaveInstanceState() and then load it back up in onCreate(). Take a look at http://developer.android.com/reference/android/app/Activity.html and http://developer.android.com/reference/android/os/Bundle.html.
If your activity has finish()'d, you're not going to avoid going through onCreate() again -- however there's no need to worry about that. Just let your onCreate() examine the contents of its intent extras, and if there are none then follow your default initialization but if there are extras, make them carry enough state data so you can be back in the previous state.
精彩评论