Drawable Landscape isn't loaded on Orientation Change - android

I want to prevent my activity from restarting when I change the orientation from portrait to landscape, so I call this on the corresponding activity on manifest:
android:configChanges="orientation|screenSize"
My activity isn't restarted anymore but unfortunately the drawables and layout I have for the activity on landscape orientation isn't loaded.
When I removed the line above, the drawables and layout are loaded correctly but then the activity restarted.
is there any way to prevent the activity from restarting while the landscape resources are still loaded correctly?

The best solution is save the state before rotation happens, let the system reload normally your activity and then repopulate it with the previously saved state. You can do that overriding onSaveInstanceState(Bundle savedInstanceState) method.
But if you want to manage the orientation change by yourself, you can read this similar question: How to refresh an Android RelativeLayout when orientation changes without restarting the Activity?

Related

Avoid reload view after orientation change

I am new to android development . I have been puzzled by this problem in recent times .
Every time orientation changes my view gets reloaded. I want to avoid this. This is happening on change from landscape to portrait and vice versa.
Put below line in your manifest in activity:
android:configChanges="orientation|screenSize"
So, your activity should look like:
<activity
android:configChanges="orientation|screenSize"
android:name=".YourActivity"/>
This way prevent destroying activity on orientation changes.
The reason why Android is reloading the view after the orientation has changed is that this view is most probably another view than the one that was displayed before the orientation change happened, e.g. with new layout, font sizes, imageviews or other resources. The Android system is able to automatically load suitable resources for different screen dimensions (e.g. height, width, density ... ) in case of an orientation change.
For this to happen, Android first calls the activity's onDestroy() method, followed by a call to its onCreate() method to enable the activity to reload resources and activity state.
If for some reason you do not want this behaviour, you can follow the above advice and add an android:configChanges attribute to your activity element. In this case Android calls the activity's onConfigurationChanged() method instead of onDestroy() and onCreate(). onConfigurationChanged() is the method to place your code to adapt to the new orientation, if necessary. If not, it can also be left empty.
For further reading, see http://developer.android.com/guide/topics/resources/runtime-changes.html

android switch layout on rotate

I have an activity with a videoview and a button, I have two different layout, one for portrait mode and second for landscape.
So, I create layout-land for second layout, and all works fine.
The problem is when rotation occours. Videoview restart becouse Activity renew.
Setting in manifest orientation|screensize video not restart, but I lose landscape layout.
How to do both tasks ?
Keep the configChanges in your AndroidManifest.xml, but also make use of onSaveInstanceState and onRestoreInstanceState to put the activity back in to the state you want it to be in after the orientation has changed.
Look here for more details

Screen goes black for a while if camera orientation is changed

While changing device orientation from portrait to landscape and landscape to portrait it is happening.The problem I am facing is while changing from one mode to another screen goes black for a while.It seems activity is recreated.
I used android:configChanges but after that programatically surface view orientation I am not able to change.
How to avoid the black screen while changing camera mode?
When activity recreated it does all things you requested in onCreate().
If you are using Activity then you should consider using onRetainNonConfigurationInstance() for the heavy objects in your activity.
But remember onRetainNonConfigurationInstance() is now deprecated.
If you are comfortable with Fragments then you should use setRetainInstance(boolean) instead.
Below links will help you.
http://developer.android.com/resources/articles/faster-screen-orientation-change.html
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/Fragment
http://android.codeandmagic.org/2011/07/android-fragments-saving-state-and-screen-rotation/

Recreating WebViews while changing the orientation in android

I have built an application and the best part is, it is running fine and the worst part is,whenever I hold the device and turn it from portrait to landscape or vice versa,the views are regenerating each and every time when the orientation changes.I have done all the possible things to the best of my knowledge i.e.
1.created layout-land folder and placed the xml file for the landscape mode.
2.Have given the following permission in the manifest:
android:configChanges="orientation|keyboardHidden"
Is there anything else I have missed out?
I do not want the changes or recreation to occur whenever I change the orientation.
By default,Android activity is recreated when orientation is changed, and xml layout used by default is xml that you have in "layout" folder unless you have a separate xml in "layout-land" for landscape mode.
On the other hand if you want to handle Orientation or any other config changes by you self and to avoid calling OnCreate() then use "android:configChanges" in manifest file and Overide OnConfigChanges in your activity.
The reason that it is recreating your views is because the oncreate is being called. This will ideally reset all your views. If you override the method that handles the orientation changes, you will catch it a lot easier and preserve your views.
Ex:
http://jnastase.alner.net/archive/2010/10/27/handling-orientation-change-in-android.aspx

Android orientation change

I am downloading an image from a server and adding it as left drawable to different buttons. The problem is when I rotate the emulator, all the left drawables of buttons are reloaded.
I also tried adding android:configChanges="orientation" to manifest file but that doesn't help.
Is there anything I am doing wrong or is there a particular procedure for it?
Whenever the screen is rotated, the Activity is restarted and all your drawables need to be loaded again.
To stop it from reloading, add this to your manifest:
android:screenOrientation="portrait"
This will keep your activity in portrait mode at all times.
If you do wish to be able to change the orientation, you can use these methods
onSaveInstanceState
onRestoreInstanceState
to keep your drawables in memory and reload them when the activity runs onResume

Categories

Resources