0
我有一個選項菜單。當活動失去焦點時,選定的選項菜單項保留狀態,但當我的活動被破壞時,所有選項都會重置。保存選項菜單共享首選項
如何從已銷燬狀態恢復後保存所選首選項的狀態?有問題可視化如何實現代碼Shared Preferences。
(只需要布爾值,但我已經包含了靜態菜單項。)
public final static int MENU_SOMETHING_MODE_ON = 1;
public final static int MENU_SOMETHING_MODE_OFF = 2;
public final static int MENU_FULLSCREEN_ON = 3;
public final static int MENU_FULLSCREEN_OFF = 4;
public final static int MENU_SOUND_ON = 5;
public final static int MENU_SOUND_OFF = 6;
public final static int MENU_FASTER = 7;
public final static int MENU_SLOWER = 8;
public final static int MENU_SOMETHING = 9;
public final static int MENU_EXTRAS = 10;
private static boolean soundOn = true;
private static boolean mFaster = true;
private boolean fullscreen = true;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_SOMETHING, 0, R.string.menu_new_something);
menu.add(0, MENU_SOMETHING_MODE_ON, 0,
R.string.menu_something_on);
menu.add(0, MENU_SOMETHING_MODE_OFF, 0,
R.string.menu_something_off);
menu.add(0, MENU_FULLSCREEN_ON, 0, R.string.menu_fullscreen_on);
menu.add(0, MENU_FULLSCREEN_OFF, 0, R.string.menu_fullscreen_off);
menu.add(0, MENU_SOUND_ON, 0, R.string.menu_sound_on);
menu.add(0, MENU_SOUND_OFF, 0, R.string.menu_sound_off);
menu.add(0, MENU_FASTER, 0, R.string.menu_faster);
menu.add(0, MENU_SLOWER, 0, R.string.menu_slower);
menu.add(0, MENU_EXTRAS, 0, R.string.menu_extras);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
menu.findItem(MENU_SOUND_ON).setVisible(!getSoundOn());
menu.findItem(MENU_SOUND_OFF).setVisible(getSoundOn());
menu.findItem(MENU_SOMETHING_ON).setVisible(
getMode() == SOMETHING_NORMAL);
menu.findItem(MENU_SOMETHING_OFF).setVisible(
getMode() != SOMETHING_NORMAL);
menu.findItem(MENU_FULLSCREEN_ON).setVisible(!fullscreen);
menu.findItem(MENU_FULLSCREEN_OFF).setVisible(fullscreen);
menu.findItem(MENU_FASTER).setVisible(getFaster());
menu.findItem(MENU_SLOWER).setVisible(!getFaster());
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case MENU_SOMETHING:
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you sure?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'YES' Button
something.new();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Something");
// Icon for AlertDialog
alert.setIcon(R.drawable.dialog);
alert.show();
return true;
case MENU_SOMETHING_ON:
setMode(THIS_SOMETHING);
return true;
case MENU_SOMETHING_OFF:
setMode(THIS_NORMAL);
return true;
case MENU_FULLSCREEN_ON:
fullscreen = true;
setFullscreen();
return true;
case MENU_FULLSCREEN_OFF:
fullscreen = false;
setFullscreen();
return true;
case MENU_SOUND_ON:
setSoundOn(true);
return true;
case MENU_SOUND_OFF:
setSoundOn(false);
return true;
case MENU_FASTER:
setFaster(false);
return true;
case MENU_SLOWER:
setSlower(true);
return true;
case MENU_EXTRAS:
startExtras();
return true;
}
return false;
}
private void setFullscreen()
{
if (fullscreen) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
mView.requestLayout();
}
public synchronized static void setMode(int newMode)
{
Mode = newMode;
}
public synchronized static int getMode()
{
return Mode;
}
public synchronized static boolean getSoundOn()
{
return soundOn;
}
public synchronized static void setSoundOn(boolean so)
{
soundOn = so;
}
public synchronized static boolean getFaster()
{
return Faster;
}
public synchronized static void setFaster(boolean dont)
{
Faster = dont;
}
感謝您的有用答案,但我更願意在當前代碼框架中保存選項菜單。 – user583922 2011-01-21 04:31:07