Android 按下后退按钮时保存状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12171320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
save the state when back button is pressed
提问by Sindu
I am developing an android app. If I press a back button the state of my application should be saved .What should i use to save the state ..am confused with all of these onPause()
,onResume()
, or onRestoresavedInstance()
??? which of these should i use to save the state of my application?? For eg when i press exit button my entire app should exit i have used finish() ?
我正在开发一个 android 应用程序。如果我按后退按钮我的应用程序的状态应该保存。什么我应该用它来保存所有的这些困惑的状态..am onPause()
,onResume()
或onRestoresavedInstance()
??? 我应该使用其中的哪一个来保存我的应用程序的状态?例如,当我按下退出按钮时,我的整个应用程序都应该退出,我已经使用了 finish() 吗?
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
s1=(Button)findViewById(R.id.sn1);
s1.setOnClickListener(this);
LoadPreferences();
s1.setEnabled(false);
}
public void SavePreferences()
{
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("state", s1.isEnabled());
}
public void LoadPreferences()
{
System.out.println("LoadPrefe");
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Boolean state = sharedPreferences.getBoolean("state", false);
s1.setEnabled(state);
}
@Override
public void onBackPressed()
{
System.out.println("backbutton");
SavePreferences();
super.onBackPressed();
}
回答by Andro Selva
What you have to do is, instead of using KeyCode Back, you have override the below method in your Activity,
您需要做的是,不要使用 KeyCode Back,而是在您的 Activity 中覆盖以下方法,
@Override
public void onBackPressed() {
super.onBackPressed();
}
And save the state of your Button using SharedPrefrence, and next time when you enter your Activity get the value from the Sharedpreferenceand set the enabled state of your button accordingly.
并使用SharedPrefence保存 Button 的状态,下次进入 Activity 时从Sharedpreference获取值并相应地设置按钮的启用状态。
Example,
例子,
private void SavePreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("state", button.isEnabled());
editor.commit(); // I missed to save the data to preference here,.
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Boolean state = sharedPreferences.getBoolean("state", false);
button.setEnabled(state);
}
@Override
public void onBackPressed() {
SavePreferences();
super.onBackPressed();
}
onCreate(Bundle savedInstanceState)
{
//just a rough sketch of where you should load the data
LoadPreferences();
}
回答by Dhaval Parmar
you can use this way
你可以用这种方式
public void onBackPressed() {
// Save settings here
};
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
当活动检测到用户按下后退键时调用。默认实现只是完成当前活动,但您可以覆盖它以执行您想要的任何操作。
save your application state in this method.
在此方法中保存您的应用程序状态。