android rotation handling - android

Is onDestroy() guaranteed to be called when I rotate the screen?
I heard that one can define onConfigurationChanged() to change this default behavior. What would usually be written in that method? And what are the other ways to change the default behavior of rotation?

Is onDestroy() guaranteed to be called when I rotate the screen?
Yes
onConfigurationChanged() will be called for any configuration change you want to handle yourself (you declare those configurations under configChanges in your manifest). I think the best would be to avoid that altogether.
You can disable rotation in the manifest by setting Orientation for your activity such as 'portrait' or 'landsacpe'
Normally if the orientation can change you will want to save and then restore some of your data using onSaveInstanceState and then restoring the data back in onCreate

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

Configuration changes

In android, when the device's orientation changes, the activity is destroyed and recreated. Is there a way to prevent this from happening without preventing orientation changes in the android manifest?
In your manifest for the main activity, add the line
android:configChanges=<x>
where x is the type of change you want to handle in your activity. For orientation, use "orientation". Then, your activity method onConfigurationChanged will be invoked and you can handle it however you want.
Probably you won't need any special handling since the layouts know how to relayout and draw themselves.
Try this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Do your operation and agin unfix orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

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.

handeling orientation change in 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.

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