简体   繁体   中英

maintain state of activity after calling it again

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

public 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 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM