Android - How to save the state of a CheckBox

前端 未结 3 1390
萌比男神i
萌比男神i 2021-01-01 05:53

I have an application with checkboxes, How can i save them?

My layout is:




        
3条回答
  •  伪装坚强ぢ
    2021-01-01 06:26

    Override onSaveInstanceState(Bundle outState) and write which ones are checked into outState. Then you can see which ones are checked when the activity is create by extracting that information from savedInstanceState passed to onCreate(Bundle savedInstanceState)

    For example,

    @Override
    protected void onSaveInstanceStat(Bundle b){
        b.putBoolean("een",een.isChecked());
        b.putBoolean("v52",v52.isChecked());
        b.putBoolean("v53",v53.isChecked());
    
        // ... etc
    }
    
    @Override
    protected void onCreate (Bundle savedInstanceState){
       // everything you have in your onCreate implementation 
    
       een.setChecked(b.getBoolean("een"));
       v52.setChecked(b.getBoolean("v52"));
       v53.setChecked(b.getBoolean("v53"));
    
        // ... etc
    }
    

    IMPORTANT: There is no guarantee that 'onSaveInstanceState' will be called. So only use this if saving the ui state is for convenience for the user and not vital to functionality. User will be happier if you don't save data on their device, ie by using SharedPreference.

提交回复
热议问题