Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Efficient Screen Rotation Handling

In Android, when the screen orientation changes (between landscape and portrait) the onCreate method is called which ,if not handled properly, will recreate the entire activity when the result is too simply change the layout and retain all of the same information.

I know of a few ways to solve this, but I'm interested in the most efficient way.

1) Telling the manifest that I will handle the orientation change by overriding the onConfigurationChanged() method and leaving it empty.

2) override onSaveInstanceState() and save the data here to be repopulated in onCreate()

3) Override onPause() which will create a bundle of the data and send it to onSaveInstanceState()

Obviously, these are all options, but which one is a better practice (small, medium, large amounts of data)

Thank you

like image 732
Sababado Avatar asked Jan 25 '12 04:01

Sababado


People also ask

How do you handle rotation on Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What is the effective approach that is used to handle screen orientation?

Lock screen orientation To lock the screen orientation change of any screen (activity) of your android application makes your activity display only in one mode i.e. either Landscape or Portrait. This is the simplest way to handle screen orientation but not generally recommended.

How do I make my Android auto-rotate faster?

Go back to the main screen of the system settings and enter the new "Developer options" menu. Scroll down (a lot) until you see the "Transition animation scale" option. Tap it and select a speed (where 1.0× is normal, 0.5× is fast and 10× is extremely slow). You can also choose to turn the animation off.

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

Just add android:configChanges="orientation|screenSize" in activity tab of manifest file. So, Activity won't restart when orientation change.


1 Answers

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running Activity (onDestroy() is called, followed by onCreate()).

  • To properly handle a restart, it is important that your activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your activity so that you can save data about the application state. You can then restore the state during onCreate() or onRestoreInstanceState().

However, you might encounter a situation in which restarting your application and restoring significant amounts of data can be costly and create a poor user experience. In such a situation, you have two other options:

  1. Retain an object during a configuration change

    Allow your activity to restart when a configuration changes, but carry a stateful Object to the new instance of your activity.

  2. Handle the configuration change yourself

    Prevent the system from restarting your activity during certain configuration changes, but receive a callback when the configurations do change, so that you can manually update your activity as necessary.

So, As per your data operation and coast of user experience with application you can choose what is best for you..

EDIT: As per your need for just change the layout I think you have to use onConfigurationChanged() and do the changes in it.

like image 69
user370305 Avatar answered Oct 03 '22 19:10

user370305