Don't reload application when orientation changes
There are generally three ways to do this:
As some of the answers suggested, you could distinguish the cases of your activity being created for the first time and being restored from
savedInstanceState
. This is done by overridingonSaveInstanceState
and checking the parameter ofonCreate
.You could lock the activity in one orientation by adding
android:screenOrientation="portrait"
(or"landscape"
) to<activity>
in your manifest.You could tell the system that you meant to handle screen changes for yourself by specifying
android:configChanges="orientation|screenSize"
in the<activity>
tag. This way the activity will not be recreated, but will receive a callback instead (which you can ignore as it's not useful for you).
Personally I'd go with (3). Of course if locking the app to one of the orientations is fine with you, you can also go with (2).
android prevent refresh on change orientation
By declaring android:configChanges="orientation|screenSize"
you instruct Activity Manager to not restart your activity and let you handle configuration change via onConfigurationChanged()
.
If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.
Source: http://developer.android.com/guide/topics/resources/runtime-changes.html
Which means that onCreate()
will be skipped on configuration changed, and you cannot swap your layout (since onCreate()
is where you recreate the view).
In your case, you want to change layout, so there is no choice but to refresh your activity, which means to remove android:configChanges="orientation|screenSize"
. If you want to keep the state, you can save and check the Bundle
passed to your onCreate()
to restore state accordingly.
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// set your layoutId according to landscape/portrait
setContentView(layoutId);
if (savedInstanceState != null) {
// restore your state here
}
...
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save your state here
}
For more reference: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Android App orientation change restarts activity
Actually you shouldn't prevent the Activity to be restarted. It is neccessary to recreate the Activity after a rotation change for several reasons. One of it is that the layout has to be inflated to deal with the changed screen size and things like that (it's easy to imagine that the layout is totally different in portrait than it is in landscape mode).
However, there is a way you can tell the system that you deal with the screen changes by yourself. Therefore change the line in your manifest
android:configChanges="orientation|keyboardHidden"
to
android:configChanges="orientation|screenSize"
The activity won't be recreated then. You'll get a callback via the onConfigurationChanged()
method so you can do something when the orientation has changed. If you don't want to do anything when the configuration has changed, just don't override the onConfigurationMethod()
in your Activity. Read this section in the Android Developers API Guide for more information.
I got this from this answer. There are two more approaches in the answer but I think the one I described above is the best in your case.
EDIT: Maybe you have to add keyboard|keyboardHidden
to the android:configChanges
attribute as well, as stated in this answer
EDIT #2: If you want to retrieve the current orientation of the device you can call
Activity.getResources().getConfiguration().orientation
which will return the constants ORIENTATION_PORTRAIT
or ORIENTATION_LANDSCAPE
.
If you're interested in the exact rotation angle, use
int rotation = getWindowManager().getDefaultDisplay().getRotation();
and implement a differentiation with something like a switch case described here.
And a third way to determine the device's rotation is getRequestedOrientation()
which will return a constant defined in the documentation
Refreshing resource after screen orientation changes
Firstly, don't use configChanges
. That's the lazy way out and will end up biting you in the future. Drawables are cached, so it's likely that this is causing issues with getting the proper image for the orientation (which would explain why it works once but not after). You could get around this by having two drawables -- one named splash_bg_port, one named splash_bg_land, and use those, switching on the orientation you receive from newConfig
.
Also, if your splash_bg is a layer-list with bitmap items, I've noticed that sometimes it doesn't pull from the correct resource folders (due to caching) after the first time you access the drawable.
Related Topics
How to Disable Action Bar Permanently
Date and Time Change Listener in Android
How to Get the Touch Position in Android
Listview Reusing Views When ... I Don't Want It To
Couldn't Load Memtrack Module Logcat Error
Android: Out of Memory Exception in Gallery
Google Maps Android API V2 Authorization Failure
Tabwidget Current Tab Bottom Line Color
Dynamically Changing the Fragments Inside a Fragment Tab Host
How to Add a Button to Preferencescreen
How to Build an Executable for Android Shell
How to Change the Text Color of Menu Item in Android
Spinner: Get State or Get Notified When Opens
How to Handle an Asynctask During Screen Rotation
How to Set a Reminder in Android
How to Extract a List of Objects from Firebase Datasnapshot on Android