0

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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Alessandro
  • 3,666
  • 2
  • 28
  • 41
  • could you add the code of the method: public boolean onOptionsItemSelected(MenuItem item) in your DetailActivity? – Christopher May 09 '16 at 11:14

2 Answers2

0

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.

Oleg Khalidov
  • 5,108
  • 1
  • 28
  • 29
  • My code is nearly identical to yours. The problem is `onCreate`'s `savedInstanceState` is always null. – Alessandro May 09 '16 at 08:34
  • I think this behavior because of `parent activity` feature. [Here](http://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly) is the issue on stackoveflow – Oleg Khalidov May 09 '16 at 08:56
0

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.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24