Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent refreshing of activity on orientation change android fragment

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.

like image 229
bharath Avatar asked Jun 09 '13 15:06

bharath


People also ask

How do you prevent data from reloading and resetting when the screen is rotated?

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.

What happens to fragment when activity is rotated?

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 .

What happens when screen orientation changes in android?

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() ).

Is it better to use fragments or activities?

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.


2 Answers

Above API level 12 we need to add screenSize also to prevent refreshing along with orientation.

android:configChanges="orientation|keyboardHidden|screenSize"
like image 152
zacharia Avatar answered Oct 05 '22 09:10

zacharia


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.

like image 27
MaciejGórski Avatar answered Oct 05 '22 07:10

MaciejGórski