保存状态时,后面布顿是pressed后面、状态、pressed、布顿是

2023-09-06 09:43:25 作者:撸至呻

我开发一个Android应用程序。如果我preSS后退按钮我的应用程序的状态应该保存.wat我应该用它来保存状态..am困惑与所有这些的onPause(),onResume(),或onRestoresavedInstance的???其中,这些我应该使用救我参加办法的状态?对于例如,当我preSS退出按钮移到了我的整个应用程序应该退出我用面漆()?

 公共无效的onCreate(包savedInstanceState)
   {

    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    S1 =(按钮)findViewById(R.id.sn1);
    s1.setOnClickListener(本);
    加载preferences();
    s1.setEnabled(假);
    }

    公共无效节省preferences()
 {
        共享preferences共享preferences = GET preferences(MODE_PRIVATE);
        共享preferences.Editor编辑=共享preferences.edit();
        editor.putBoolean(状态,s1.isEnabled());
       }
 公共无效负载preferences()
 {
     的System.out.println(加载preFE);
        共享preferences共享preferences = GET preferences(MODE_PRIVATE);
        布尔州=共享preferences.getBoolean(国家,假);
        s1.setEnabled(州);
       }
 @覆盖
 公共无效onBack pressed()
 {
    的System.out.println(后退按钮);
    节省preferences();
     super.onBack pressed();
 }
 

解决方案

什么,你需要做的是,而不是使用密钥code后退,你有你的活动覆盖以下方法,

  @覆盖
公共无效onBack pressed(){

    super.onBack pressed();
}
 

和使用保存按钮的状态的共享prefrence ,然后下一次,当你进入你的活动获得的价值从共享preference 并设置相应的按钮的启用状态。

例如:

 私人无效节省preferences(){
    共享preferences共享preferences = GET preferences(MODE_PRIVATE);
    共享preferences.Editor编辑=共享preferences.edit();
    editor.putBoolean(状态,button.isEnabled());
    editor.commit(); //我错过了在这里将数据保存到preference ,.
   }

   私人无效载荷preferences(){
    共享preferences共享preferences = GET preferences(MODE_PRIVATE);
    布尔州=共享preferences.getBoolean(国家,假);
    button.setEnabled(州);
   }

   @覆盖
公共无效onBack pressed(){
    节省preferences();
    super.onBack pressed();
}

的onCreate(捆绑savedInstanceState)
{
   你应该加载数据的//只是一个粗略的草图
    加载preferences();
}
 
蜗牛C款从0开始折腾备记

I am developing an android app . if i press a back button the state of my application should be saved .wat 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 appplication ??For eg when i press exit buttton my entire app should exit i have used 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();
 }

解决方案

What you have to do is, instead of using KeyCode Back, you have override the below method in your 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 Sharedpreference and set the enabled state of your button accordingly.

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();
}