When would one justify adding this to manifest.xml?
android:configChanges="orientation|keyboardHidden|screenSize"
Thankyou
Yes its required based on your requirement. I had an app in which we had to show videos on orientation change the video would restart hence i had to overide the onConfigureation() method and handle it, As i said it depends on your requirement
Adding this to the manifest means that you will control what happens when orientation changes, keyboard is minimized, screen size changes. If you do not add this then the onCreate gets called again on these events.
But say you have a different layout for landscape and portrait. Then you would like to call onCreate again to render the landscape layout.
Related
I have a DialogFragment in my Android app, and I need to set android:configChanges="orientation|screenSize, I want to load different layout for my DialogFragment in portrait or landscape, but now Android will not automatically do this for me, I think I need to update the layout in onConfigurationChanged , but I don't know how to do it, any sample code will be better, thank you!
Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest.xml. Using this attribute your Activities won’t be recreated and all your views and data will still be there after orientation change.
try this code and changed your configuration :
<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
This attribute informs to Android system that you are going to handle orientation and screenSize changes for this Activity. So instead of destroying and recreating your Activity, Android will just rotate the screen and invoke one of the lifecycle callback method which is onConfigurationChanged(Configuration).
try this
I finnally find my solution, but I think it maybe only work for my situation, my situation is I want to show a imageView in portrait and not show in landscape, so I set the visibility as visiable or gone in function onConfigurationChanged' and it works
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
I am having a requirement where there are several buttons in a activity, and clicking on one will change the background. The activity refreshes on orientation change. Then i included, android:configChanges="keyboardHidden|screenSize"
which solves the problem, but i have separate screens for portrait and landscape modes, this saves the state and do not pick the layout for landscape when the orientation is changes. Please guide how i can change the orientation but still prevent the activity refresh and thus saving the clicked button and background color.
dont use android:configChanges="keyboardHidden|screenSize"
and use onSaveInstanceState() for return color or resource id of background and then check it in oncreate() for set background
this example help you
How to use onSavedInstanceState example please
used this
android:configChanges="keyboardHidden|orientation|screenSize"
I use data of the accelerometer in my app. If the device´s angle is changed the screen orientation should change.
In order to change the orientation I have 2 layouts, one for portrait and one for landscape.
When the orientation changes a function is called that changes the layout with:
setContentView(R.layout.landscape); or setContentView(R.layout.portrait);
This works fine but I have a problem with several UI elements like buttons or ToggleButtons.
I initialize a onClickListener in the onCreate Method for them and each time the orienation gets changed I initialize a new onClickListener.
Unfortunately I can´t change the state of ToggleButtons anymore.
How can I solve this problem?
you can use /layout-land and /layout-port and the phone will automatically switch them for you on orientation change. see
http://developer.android.com/guide/practices/screens_support.html#DesigningResources
as for the toggle buttons, see onRetainNonConfigurationInstance()
http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29
As described by Bill Gary above, why not just let the system take care of orientation changes for you? Also the standard system widgets should automatically preserve their state when you do this. See here for why and how to manage custom state, if you need to.
Is it possible in Android to capture screen rotate events but not actually have the activity respond to the rotate events?
To make things more difficult, I've got an activity backed by a TabActivity, and in one of the tabs I want to rotate some of the content area on screen - but leave the tab in portrait mode.
I've tried setting the activity orientation to portrait in the manifest, but then I no longer get the onConfigurationChanged(..) events. However, If I remove this line, the whole tab rotates as well.
Any help appreciated!
You might want to add "orientation" to activity's configChanges property in AndroidManifest.
android:configChanges="orientation"
http://androidappdocs.appspot.com/guide/topics/manifest/activity-element.html#config
Set the orientation to portrait mode in the manifest as you did, but listen directly to the accelerometer sensor (which is the sensor from which Android derives the orientation of the device) updates and parse them yourself to know when the orientation changed.
It's actually quite easy to do. See here for an example: http://www.anddev.org/accessing_the_accelerometer-t499.html