In my Android application, when I rotate the device (slide out the keyboard) then my Activity
is restarted (onCreate
is called). Now, this is probably how it's supposed to be, but I do a lot of initial setting up in the onCreate
method, so I need either:
- Put all the initial setting up in another function so it's not all lost on device rotation or
- Make it so
onCreate
is not called again and the layout just adjusts or - Limit the app to just portrait so that
onCreate
is not called.
Instead of trying to stop the
onCreate()
from being fired altogether, maybe try checking theBundle
savedInstanceState
being passed into the event to see if it is null or not.For instance, if I have some logic that should be run when the
Activity
is truly created, not on every orientation change, I only run that logic in theonCreate()
only if thesavedInstanceState
is null.Otherwise, I still want the layout to redraw properly for the orientation.
not sure if this is the ultimate answer, but it works for me.
The approach is useful but is incomplete when using Fragments.
Fragments usually get recreated on configuration change. If you don't wish this to happen, use
setRetainInstance(true);
in the Fragment's constructor(s)This will cause fragments to be retained during configuration change.
http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)
Put the code below inside your
<activity>
tag inManifest.xml
:You might also consider using the Android platform's way of persisting data across orientation changes:
onRetainNonConfigurationInstance()
andgetLastNonConfigurationInstance()
.This allows you to persist data across configuration changes, such as information you may have gotten from a server fetch or something else that's been computed in
onCreate
or since, while also allowing Android to re-layout yourActivity
using the xml file for the orientation now in use.See here or here.
It should be noted that these methods are now deprecated (although still more flexible than handling orientation change yourself as most of the above solutions suggest) with the recommendation that everyone switch to
Fragments
and instead usesetRetainInstance(true)
on eachFragment
you want to retain.In the activity section of the
manifest
, add:Even though it is not "the Android way" I have gotten very good results by handling orientation changes myself and simply repositioning the widgets within a view to take the altered orientation into account. This is faster than any other approach, because your views do not have to be saved and restored. It also provides a more seamless experience to the user, because the respositioned widgets are exactly the same widgets, just moved and/or resized. Not only model state, but also view state, can be preserved in this manner.
RelativeLayout
can sometimes be a good choice for a view that has to reorient itself from time to time. You just provide a set of portrait layout params and a set of landscaped layout params, with different relative positioning rules on each, for each child widget. Then, in youronConfigurationChanged()
method, you pass the appropriate one to asetLayoutParams()
call on each child. If any child control itself needs to be internally reoriented, you just call a method on that child to perform the reorientation. That child similarly calls methods on any of its child controls that need internal reorientation, and so on.