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.
Prevent Activity to recreated Most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change. Save this answer.
Fragments — Scenario 3: Activity with retained Fragment is rotated. The fragment is not destroyed nor created after the rotation because the same fragment instance is used after the activity is recreated. The state bundle is still available in onActivityCreated .
Some device configurations can change during runtime (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate() ).
We can't create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity. While Using fragments in the project, the project structure will be good and we can handle it easily.
Above API level 12 we need to add screenSize also to prevent refreshing along with orientation.
android:configChanges="orientation|keyboardHidden|screenSize"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With