Android : Shared preferences inside tabhost not wo

2019-08-19 08:24发布

问题:

I have a tabhost with three activities and I want to save the pressed state of the buttons of each activity

So now How can I save the pressed state of each button in all three child activities so that when I move from one activity to the other the button pressed state will be reflected on moving back. first activity -> all 4 buttons pressed -> go to 2nd activity -> come back to first activity -> all buttons in first activity should be in pressed state

When I go to second child tab and come to the first child tab the change(The buttons which I pressed are not in pressed state) is not reflecting

Help is always appreciated , Thanks

this is my code in first tabhost child activity

         @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    seatdirnbtn.setOnClickListener(listner1);
    seatdirnbtn1.setOnClickListener(listner2);
        seatdirnbtn.setPressed(true);
        seatdirnbtn1.setPressed(true);
        this.LoadPreferences();

       }

    private void SavePreferences() {

    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("sharedPreferences",MODE_WORLD_READABLE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("state", seatdirnbtn.isEnabled());
    editor.putBoolean("state1", seatdirnbtn1.isEnabled());
    editor.commit();

}

private void LoadPreferences() {
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("sharedPreferences",MODE_WORLD_READABLE);
    Boolean state = sharedPreferences.getBoolean("state", false);
    Boolean state1 = sharedPreferences.getBoolean("state1", false);
    seatdirnbtn.setPressed(state);
    seatdirnbtn1.setPressed(state1);
}

         @Override
     protected void onStart() {
    super.onStart();
        LoadPreferences();
        }

        @Override  
       protected void onPause() {
    SavePreferences();
    super.onPause();
      }

       public static boolean isclick = false;
private View.OnClickListener listner1 = new View.OnClickListener() {

    public void onClick(View v) {

        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

        } else {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
        }

        isclick = !isclick;
    }

};

private View.OnClickListener listner2 = new View.OnClickListener() {

    public void onClick(View v) {

        if (isclick) {
            seatdirnbtn1.setBackgroundResource(R.drawable.icon2hlt);
        } else {
            seatdirnbtn1.setBackgroundResource(R.drawable.icon2);
        }

        isclick = !isclick;
    }

};

回答1:

probably you should override onResume() method in which you should set buttons states. this method is called after onCreate() and even the activity is already created. If you have activities in tabHost they are not created each time you switch between tabs so onCreate() method will be called only once but onResume() every time you switch to tab with particular activity.

your code which is loading preferences is in onStart() method. Look here on activity lifecycle. You can see that this method is called only if your activity was stopped before but will never called if it was just paused.

EDIT:

if you have just 2 states like in your code from question it could be better to use ToggleButton which also generally have 2 states. You can style it to have different backgrounds for each state. This tutorial could be helpfull.

Than you will have a little bit different Listener:

    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            if(checked) {
                //do sth if it's checked
            } else {
                //do sth if it's not checked;
            }

        }
    });

to change states for them programatically:

toggleButton.setChecked(true); //or false

so finally you can save this state to SharedPreferences:

editor.putBoolean("toggleButton1",toggleButton.isChecked());

and when you will need this state:

boolean isChecked = sharedPreferences.getBoolean("toggleButton1",false);
toggleButton.setChecked(isChecked);

selector will take care of switching button backgrounds for each state.