简体   繁体   中英

Going to the same “instance” of an Intent/Activity in Android

I have a menu in an Android application, and when I click one of the buttons to start a new activity I want the instance variables to keep their values even if I go back to the menu and start it again. This is what I've tried:

public void onClick(View v) {

            Bundle b = new Bundle();
            b.putBoolean("isFav",false);
            centralsIntent = new Intent("kth.blod.CENTRALS");
            centralsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            centralsIntent.putExtras(b);

            startActivity(centralsIntent);
        }

And in the manifest:

android:launchMode="singleTop"

I think shared preferences are the easiest way to keep track of values you have set in one part of your application that you want to save and possibly modify upon returning from another part of the application. See this link for Shared Preferences . See this link to see how to implement them. Good luck!

I'm confused by your question. When you say 'I have a menu', do you mean that the main activity (or first activity) is a menu? If that is the case, the user can select from the menu and your can start a new activity which then appears on top of the menu activity. As long as you don't call finish() on the menu activity it won't go away (in the normal case) and when the user returns to it all the instance variables will still contain whatever they had when the user started the new activity. Also, you shouldn't need launchMode='singleTop' for this as this is the standard default behaviour.

Or maybe I don't completely understand what your problem is...

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