android - Maintain state between activities -
my app has 2 activities: masteractivity
, detailactivity
. masteractivity
has 2 visualization modes: list mode , map mode. action bar item toggles between them.
i'd mantain selected visualization mode when user goes detailactivity
, comes back. in beginning used sharedpreferences
user previous visualization mode after device boot or long inactivity time, , that's not mean.
then switched bundle
, onsaveinstancestate
but, when user clicks on button of detailactivity
, oncreate
's bundle
empty can't restore previous visualization mode , reverts list one.
app uses toolbar
, androidmanifest.xml
configured 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 you. code 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 use androjeta framework (maintained me). comes number of features including @retain
can use in case:
class masteractivity extends baseactivity { @retain int mode; @override protected void onresume() { super.onresume(); if(mode == 1) { //list mode } else { // map mode } } }
note here masteractivity
extends baseactivity
need create too. please, follow link details.
Comments
Post a Comment