ToggleButton change state on orientation changed

狂风中的少年 提交于 2019-12-11 12:48:33

问题


I have a CustomButton class (extends LinearLayout) where I inflate a layout which contains a ToggleButton (in reality this is more complex, but I simplified here the problem).

public class CustomButton extends LinearLayout {

    private ToggleButton toggleOnOffButton;

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.custom_button_layout, this);
    }

    @Override
    protected void onFinishInflate() {
        toggleOnOffButton = (ToggleButton) findViewById(R.id.toggle_on_off_button);
        super.onFinishInflate();
    }

    public ToggleButton getToggleOnOffButton() {
        return toggleOnOffButton;
    }
}

custom_button_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

    <ToggleButton android:id="@+id/toggle_on_off_button"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textOff="Off"
                  android:textOn="On"
                  android:layout_alignParentRight="true"
            />
</RelativeLayout>

I have an activity where I inflate an layout with 2 CustomButton-s. The on/off state of the first toggleButton is saved in shared preferences and I load the value from there in onCreate method.

public class FirstActivity extends Activity
{
    private CustomButton customButton;
    private ToggleButton toggleBut;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        customButton = (CustomButton)findViewById(R.id.toggleButton);
        toggleBut = customButton.getToggleOnOffButton();

        boolean saved = loadPreferences("toggleBut");
        toggleBut.setChecked(saved);
        toggleBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = toggleBut.isChecked();
                savePreferences("toggleBut", checked);
            }
        });
    }

    private void savePreferences(String key, boolean value){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private boolean loadPreferences(String key){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, true);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"> 
    <com.example.example.cs.ssd.custom.CustomButton
            android:id="@+id/toggleButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

    <com.example.example.cs.ssd.custom.CustomButton
            android:id="@+id/toggleButton2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
</LinearLayout>

When I start the application the first toggleButton is ON. When I change the orientation of the screen, automatically the first toggleButton become Off, even saved has value true and is called toggleBut.setChecked(saved); and I think this has to do with the CutomButton I've created because if the main.xml layout contains only 1 CustomButton this problem does not reproduce. I'm not sure what I'm doing wrong... Here is the archive with the above code (as a project): archive


回答1:


If you want your CustomButton to retain its current state after an orientation change simply override onSaveInstanceState() and onRestoreInstanceState().

A Solution

I ran through your code and noticed that toggleBut's state was being changed after onActivityCreated() but before onStart(). To avoid having any of these methods override your toggle settings, I simply moved these lines from onViewCreated():

boolean saved = loadPreferences("toggleBut");
toggleBut.setChecked(saved);

and put them in onResume(). Hope that helps!

Better Solution

Your ToggleButton setting are being overwritten when the system tries to restore the default saveInstanceState, probably in Fragment.onActivityCreated().

In CustomButton, override these functions like so:

@Override
protected Parcelable onSaveInstanceState() {
    Bundle state = new Bundle();
    state.putParcelable("default", super.onSaveInstanceState());
    state.putParcelable("toggle", toggleOnOffButton.onSaveInstanceState());
    return state;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    Bundle bundle = (Bundle) state;
    super.onRestoreInstanceState(bundle.getParcelable("default"));
    toggleOnOffButton.onRestoreInstanceState(bundle.getParcelable("toggle"));
};

Understand that the system will still change the ToggleButton states, without the one more thing. But let me try to explain what;s happening:

  • onActivityCreated(Bundle savedInstanceState) passes it's savedInstanceState to every layout element by calling 'onRestoreInstanceState(Bundle savedInstanceState)`.
  • onRestoreInstanceState() begins with the layout's root element first and traverses up the layout's hierarchy (in this case it sets the checked state of each ToggleButton last).
  • Since the default methods are clearly not working, we need to define our own save / restore method for the ToggleButtons. Otherwise any changes we make before the system calls onRestoreInstanceState() will be changed again by the system...

So, lastly we will exclude the ToggleButtons from this default behavior by adding the following line to CustomButton.onFinishInflate():

toggleOnOffButton.setSaveEnabled(false);

Voila, your CustomButtons automatically retain their state.



来源:https://stackoverflow.com/questions/11213864/togglebutton-change-state-on-orientation-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!