简体   繁体   中英

Maintain state between activities

My app has two activities: MasterActivity and DetailActivity . MasterActivity has two visualization modes: list mode and map mode . An action bar item toggles between them.

I'd like to mantain the selected visualization mode when user goes into DetailActivity and comes back. In the beginning I used SharedPreferences but then user would get back his previous visualization mode even after a device boot or a long inactivity time, and that's not what I mean.

Then I switched to Bundle and onSaveInstanceState but, when user clicks on back button of DetailActivity , onCreate 's Bundle is always empty so I can't restore the previous visualization mode and it always reverts to the list one.

App uses a Toolbar and AndroidManifest.xml is configured like that:

<activity
    android:name=".ui.MasterActivity"
    android:label="@string/title_activity_master"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".ui.DetailActivity"
    android:parentActivityName=".ui.MasterActivity"
    android:theme="@style/AppTheme.NoActionBar">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="it.returntrue.revalue.ui.MasterActivity" />
</activity>

Not sure why onSaveInstanceState doesn't work for you. Your code would be like:

class MasterActivity extends Activity {
    private int mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null)
            mode = savedInstanceState.getInt("mode");
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(mode == 1) {
            //list mode
        } else {
            // map mode
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("mode", mode);
    }
}

Nevertheless, consider to use Androjeta framework (maintained by me). It comes with a number of features including @Retain which you can use in your case:

class MasterActivity extends BaseActivity {
    @Retain
    int mode;

    @Override
    protected void onResume() {
        super.onResume();

        if(mode == 1) {
            //list mode
        } else {
            // map mode
        }
    }
}

Note that here MasterActivity extends from BaseActivity so you need to create that too. Please, follow the link for details.

I have two approaches. It's basically the same, but different on how you store the data

  1. Use a Singleton
  2. Keep your sharedprefs model as it is

On both occasions, delete the values on the Activity's onDestroy method.

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