handeling orientation change in android - android

I wanted to have a differnet look to one of my activities when the orientation changes.
I created the layout-land folder and added activity1.xml there but when I switch the orientation I still get the regular activity.
Is there anything else I need to do to make it work?
Thanks.

Do you have android:configChanges attribute defined in your manifest?
There are two ways the orientation change can be handled:
You define attribute android:configChanges="orientation" in your app's mainfest and implement onConfigurationChanged() in your activity. This method will then be called when orientation changes.
You do NOT define android:configChanges attribute. The you activity will be restarted (a new activity crated) and will go through a lifecycle process (onCreate, onStart,
etc), every time orientation of device changes.

Read here about configuration changes. You can override onConfgiratationChanged() method of your activity and set a content view to the activity1 layout.

Related

Android loadout/layout-land onConfigurationChanged

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

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

Change layout without calling onCreate

I have an activity with two layout files, one each for portrait and landscape modes. I see that the onCreate method is called each time I change orientation. I tried using
android:configChanges = "orientation"
in the manifest file, but it only reads the portrait layout. Is there a way I can set the layout to the landscape mode without calling onCreate?
If you use android:configChanges = "orientation", you must override the onConfigurationChanged(...) method of your Activity. Check the newConfig parameter for the new orientation then manually set the layout you want.
See the following links:
onConfigurationChanged(Configuration newConfig)
Handling the Configuration Change Yourself
You could create two layout folders. One for portrait mode and one for landscape.
layout and layout-land.
This is a fact of life with android. You need to develop your activity around the fact it can be destroyed at any minute due to a configuration change like the screen's orientation changing.
Save all the the information used to build the screen in on pause and recreate it in onCreate.
If you want your activity be only in landscape mode, you set this attribute for the activity in the AndroidManifest.xml:
android:screenOrientation="landscape"
You can put your layout.xml files in such folders for different screen orientations:
res\layout-land
res\layout-port

When is it NOT okay to use onRetainNonConfigurationInstance()

Documentation of onRetainNonConfigurationInstance() says "This function is called purely as an optimization, and you must not rely on it being called."
Under what circumstances will onRetainNonConfigurationInstance() not be called?
Some background on what I am trying to achieve: I want to my activity to be notified of orientation change reliably (but also want activity to be killed & restarted as usual).
if you want the activity to restart (calling onCreate->onResume) again on orientation change you don't place configChanges="orientation" in the manifest. you can check the orientation of the screen in the onCreate method of your activity which is probably what you need. if you dont want the activity to restart itself but rather just switch orientation then you add the configChanges flag in the manifest for the activity and then only onConfigurationChanged() will be called where you also can get the screen orientation. the second way is good when you have expensive operations running in the onCreate methods (starting threads quering databases etc) and you want to reuse that info/data for portrait and for landscape. even if you want you can change the layout in onConfigurationChanged() the same way its done in on create but you again need to find the references to all the views because their ids are not the same in that case.
In any case if you want to have a reference to something that existed before the orentation change the configChanges way is better for handling the changes but it requires a bit more work if you are changing layouts or something.

Does android reuse views during a orientation change

Does android reuse views during a orientation change?
I have a progress bar in my activity. I set the progress to 50%, and then do an orientation change. But for some reason, the progress bar maintain 50% after the orientation. I have call setprogress(0) in the onCreate() of my activity. And I have put 'printf' in anywhere I call setProgress(), i don't still don't understand why the progress bar maintains 50% everytime I do an orientation change?
Thank you for any help.
Your Activity gets torn down and recreated, but Views with IDs set save their instance state to pass through to their future selves after they are recreated. This is restored in onRestoreInstanceState. Since onRestoreInstanceState is called after onCreate, the value you set in onCreate is overwritten by the previously saved data.
If you want to manipulate your views after this step happens, onPostCreate may be what you're looking for.
If you'd like to know more about how this process works (potentially to implement this save/restore behavior in your own custom views), see View#onSaveInstanceState and View#onRestoreInstanceState
looks like you are supressing the onCreate method on a configuration change for the configuration including orientation/keyboardHidden in your manifest file. remove the android:configChanges from your activity in the manifest XML, and remove the public void onConfigurationChanged(Configuration newConfig) method from your activity.
Or, just set the progress of the progressbar to 0 in your onConfigurationChanged method
if you are implementing the onConfigurationChanged method, make sure you have it defined in the manifest XML file for the desired activity.
for example:
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboardHidden" />
onConfigurationChanged will then be called when orientation or keyboardHidden configurations occur

Categories

Resources