Landscape to Portrait recreates activity? - android

I'm allowing the user to work in both landscape & portrait modes but when I switch between these two modes it just brings the activity to its starting point.
For Example; I enter all the specifications for which I want to view the data in some activity but when I switch the mode from landscape to portrait or portrait to landscape it just brings the activity to its starting point & I've to enter all the specifications again to view my data.
Is there any solution to it or will android always re-create the activity on switching between two modes?
Thanks. :)

This is the expected behavior by default. It is reasonably easy to handle this situation. Basically you save your Activity state (and data), then repopulate your Activity when the screen is rotated (and the Activity is recreated).
Here is a good SO with the solution:
Handle screen rotation without losing data - Android
also worthwhile to read the docs on this subject (and understand the Activity lifecycle in Android):
http://developer.android.com/training/basics/activity-lifecycle/index.html

That happens because on orientation change the activity is destroyed and re-created. A way to avoid this could be by adding the following line inside your activity tag in manifest.xml
android:configChanges="orientation|screenSize"
Example :
<activity
android:name="MyActivity"
android:configChanges="orientation|screenSize">
</activity>

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.

How to prevent WebView auto refresh when screen rotation

My application - Activity with WebView was auto refresh when screen rotation and my activity back to the first 1.
Example: WebView handle 3 activity(A, B, C) when I switching from A->B or B-C then it will back to A when screen is rotating.
My question: how can we keep activity alive event screen rotation?
There is more than one approach to tackle this problem.
The easy way out is to add android:configChanges="orientation|screenSize" to the relevant Activity in your manifest. By setting this flag you tell Android to not destroy the Activity and that you're going to handle all orientation changes (if any) yourself.
The more modern approach would be to put the WebView inside a Fragment and make it retain its instance, using the setRetainInstance(true) flag. The hosting Activity will still get destroyed on orientation changes, but the Fragment containing the WebView will simply be detached and re-attached, without the need to re-create itself. You can find an example of this feature in the API demos. Keep in mind that the support library offers a pre-Honeycomb compatible implementation of fragments, so don't be fooled by the API level of the 'regular' Fragment class.
Highlighting #kirgy comment, You have to add orientation|screenSize to your manifest if your API > 3.2 , It wont work without it in some cases.
Add android:configChanges="orientation" to your manifest file to prevent restarts when the screen orientation changes.
like::
<activity android:name=".MyActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name">
See this for some references.

Android Parent and Child ActivityOrientation

While running a landscape activity and a popup dialog is displayed, which is in a portrait mode, I've noticed that the parent activity was also rotated to portrait mode. Is it not possible to set the orientation of the parent and child activity independently?
EDIT: It is not actually a child activity. I am sorry if I've used wrong terminology. It is actually a different activity/application which is loaded while the 1st activity/application is still running. What I have noticed that it is happening in case of the following settings,
1st activity: android:screenOrientation="landscape"
2nd activity(dialog msg): android:screenOrientation="portrait"
Thanks,
artsylar
You can apply rotation transformations to child views, but you'll have to take over control of portrait landscape yourself entirely. There's no built-in way to do that.

Orientation issue with landscape mode

I am displaying Timer on a screen .
After starting the Timer , I am changing the orientation from the portrait to landscape mode. In order to avoid the timer getting reset when changing from the portrait to landscape mode , I am adding the following code to the activity declaration in the manifest:
android:configChanges="orientation"
The problem I am facing is that , when the screen is switching from the portrait to landscape mode , the layout which gets displayed is same as portrait & not in landscape mode. Only a part of the screen of the landscape orientation is being occupied.
Is there any way by which we can avoid resetting the timer from changing from the portrait to landscape mode & also have a proper landscape layout?
Kindly provide your inputs.
Thanks in advance.
There is a simple way of doing this.
if you have two different layouts for landscape and portrait then let the android handle all the stuff for you. i mean do not override methods onConfigurationChange() method unless strictly required and do not add android:configChanges="orientation". just make different folders for portrait mode and landscape mode. viz. layout and layout-land....
To save present state of views use bundle. whenever orientation changes android reload activity and call onCreate() method. This saved bundle is passed in onCreate() method. Now you can retrieve views' state from this bundle.
now next question will be how to use this bundle. Then here is the quick example.
override onSavedInstanceState() method to save bundle.
Thanks.
Is there any way by which we can avoid
resetting the timer from changing from
the portrait to landscape mode?
Yes, store the value somewhere during onPause and read it during onResume.
Also have a proper landscape layout
Read up on Providing Resources, in particular table 2's "Screen orientation" section which also links to a very nice page about handling runtime changes.
All you need to do is make the manifest file set the screenOrientation="landscape"
<activity android:name=".main"
android:label="#string/app_name"
android:screenOrientation="landscape">
<intent-filter>
That turns off the auto sensor on only the form that you name to have landscape.

android application containing activities with different orientation

I have an activity group say mainActivity which contain 4 activity groups. say one, two, three and four.
All activities except fourth one has orientation portrait. The fourth activity group's orientation varies it may be portrait or landscape.
when i change fourth once orientation from portrait to landscape whole application restarts.
Is it possible to do such kind of things. that mainActivity group has orientation portrait and its child orientation is landscape. And also to allow to change child's orientation without restarting application.
thanks
You could prevent the activity from restarting by adding android:configChanges="orientation" to your activity in AndroidManifest file. But it's generally not advisable to do that as stated in the documentation:
Using this attribute should be avoided
and used only as a last-resort. Please
read Handling Runtime Changes for more
information about how to properly
handle a restart due to a
configuration change.
Source: http://developer.android.com/guide/topics/manifest/activity-element.html#config

Categories

Resources