How To Maintain A Button Style And Click State After Screen Rotation?
I am trying creating an app on Android. When a user click on a button, the background color of the button changes to red. However, when I rotate the screen, the background color ch
Solution 1:
Maintain a boolean
that changes on the onClick
of the button and save it on onSaveInstanceState
like
@OverridepublicvoidonSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putBoolean(ANSWER_ONE_BUTTON_ISCLICKED, isButtonOneClicked);
super.onSaveInstanceState(savedInstanceState);
}
and on onCreateView
check like this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState)
{
if (savedInstanceState != null)
{
if (savedInstanceState.containsKey(ANSWER_ONE_BUTTON_ISCLICKED))
{
if (savedInstanceState.getBoolean(ANSWER_ONE_BUTTON_ISCLICKED))
button.setBackgroundResource(R.drawable.button_red);
else
button.setBackgroundResource(R.drawable.original_color);
}
//some codes to make the button becomes clicked.
}
}
Post a Comment for "How To Maintain A Button Style And Click State After Screen Rotation?"