Reputation: 953
I am developing an android application where, there i replace fragments on clicks. I have separate designs for landscape and portrait modes. But the problem is, when i change the orientation, the activity refreshes and the button remains unclicked, i tried giving
android:configChanges="orientation|keyboardHidden"
But no use, alse setting it to screensize was not taking the other layout designs, please suggest how to overcome this, by not refreshing the activity on orientation change but still accepting the other layout.
Upvotes: 5
Views: 2304
Reputation: 26
As Gorski said , onSaveInstanceState & onViewStateRestored ( for fragments ) is the way to store state of Fragment or Activity
onSaveInstanceState : called when the fragment is paused or stopped
onViewStateRestored : called when fragment is comeback
Ex:
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelableArrayList(Keys.CURRENT_OPERATIONS.name(), operations);
firstCall = true;
super.onSaveInstanceState(outState);
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
if (firstCall)
operations = savedInstanceState.getParcelableArrayList(Keys.CURRENT_OPERATIONS.name());
super.onViewStateRestored(savedInstanceState);
}
Upvotes: 1
Reputation: 1083
Above API level 12 we need to add screenSize also to prevent refreshing along with orientation.
android:configChanges="orientation|keyboardHidden|screenSize"
Upvotes: 9
Reputation: 22232
Use onSaveInstanceState
to store the state of button or any other state and restore it in onCreate
using provided Bundle
.
If you are using a button that is a CompoundButton
(like CheckBox
), its state will be retained automatically for you.
Upvotes: 1