Android 菜单中单选按钮上的SaveInstanceState
我想知道如何使用SaveInstanceState保存主菜单中单选按钮的状态,这样即使在应用程序关闭后,无论是否单击按钮,它都会保存Android 菜单中单选按钮上的SaveInstanceState,android,save,radio-button,Android,Save,Radio Button,我想知道如何使用SaveInstanceState保存主菜单中单选按钮的状态,这样即使在应用程序关闭后,无论是否单击按钮,它都会保存 <group android:checkableBehavior="single" android:icon="@drawable/ic_invert_colors_white_24dp" android:id="@+id/invert" android:title="Invert"> <item
<group
android:checkableBehavior="single"
android:icon="@drawable/ic_invert_colors_white_24dp"
android:id="@+id/invert"
android:title="Invert">
<item
android:id="@+id/mYellow"
android:orderInCategory="1"
android:title="Amber/Teal"
app:showAsAction="never" />
<item
android:id="@+id/mGreen"
android:orderInCategory="2"
android:title="Green/Mango"
app:showAsAction="never" />
<item
android:id="@+id/mRed"
android:orderInCategory="3"
android:title="Crimson/Indigo"
app:showAsAction="never" />
<item
android:id="@+id/mBlue"
android:orderInCategory="4"
android:title="Blue/Magenta"
app:showAsAction="never" />
</group>
保存状态,即使应用程序关闭
要实现此方案,必须将状态保存在SharedReferences中
使用以下代码:
public static void setSharedPrefString(Context context, final String key, final String value){
SharedPreferences radioState = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = radioState.edit();
editor.putString(key, value);
editor.apply();
}
public static String getSharedPrefString(Context context, final String key, final String defaultValue){
SharedPreferences radioState = PreferenceManager.getDefaultSharedPreferences(context);
return radioState.getString(key, defaultValue);
}
将单选按钮的状态另存为键的值
键:它可以是任何静态的最终字符串,如“radioStaste”,如Json键
谢谢。我想我们需要看到更多的代码…@Harrimf:对于这个->“即使在应用程序关闭后”你到底想要什么?应用程序关闭后保存状态或设备方向改变时保存状态?请添加更多代码,这是您的选项菜单吗?@MamataGelanee我的意思是当应用程序关闭时,我将尝试应用它@哈里夫:如果您不知道“接受答案”,请检查->