Android orientation change - android

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

Related

My app is not working properly when screen is rotated

I am creating an app on android studio. The app is working fine but whenever I rotate the screen, the layout is changing, the items on the screen are getting overlapped and sometimes the app restarts.
What are the possible solutions? Is preventing the screen from rotation a good option? Please help. Thanks in advance.
You need to set orientation in AndroidMenifest.xml.Use keyboardHidden attribute if you are getting any issue in keyboard.
You can set you activity's screen orientation to portrait, by this your activity will never rotate, but if you want to support landscape version then you have to create a landscape layout as well and put the layout in layout-landscape folder, but you have to take care of your data also.
Choice is yours
You can prevent your activity to recreate when orientation changes. Just add this in your activity tag in Manifest file.
<activity
android:name=".YOUR_ACTIVITY_NAME"
android:configChanges="keyboardHidden|orientation|screenSize" />
Android Activites start from the beginning when you rotate a device. So your Activity Lifecycle methods like
onCreate(), onStart(), onResume()
will be called again.
There are two possible solutions, the first one is by Rob here.
<activity
android:name=".ACTIVITY"
android:configChanges="keyboardHidden|orientation|screenSize" />
This will handle the orientation changes itself.
Another solution is to create different layout types in different layout folders. The folders will be
layout-land, layout-sw600dp-land(for 7inch tablets) and layout-sw720dp-land(for 10inch tablets)
So when you rotate a device, the layouts in this folders will be automatically inflated by the system. There will be cases like when you want to preserve the current state of the activity, for this refer to this link
No, preventing the orientation to be landscape is not a good solution. Many tablets are landscape-only, and your app won't display correctly on those.
The correct thing to do is to save your state in onSaveInstanceState() and then read it in onCreate() when the activity is re-created (the savedInstanceState variable will not be null). For this to work, the data that you are saving must be Serializable or Parcelable.
If you want to save non-serializable data or you have something running in the background, you can use a Fragment with setRetainInstance(true); and no layout. See this link.
The view will be created again, yes, but you will set the saved values so that it will look as it was before the rotation.

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

Drawable Landscape isn't loaded on Orientation Change

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?

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

Categories

Resources