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
Related
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.
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>
I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.
My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.
The same error occurs if I start in landscape and try to move to portrait later on.
It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?
Also. I'm already using android:configChanges="orientation" in my manifest for the activities.
If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.
#Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(...);
}
Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
Using android:configChanges="orientation" means you will be handling the orientation change in code. If you want it to automatically switch layouts, you shouldn't have that there.
Make sure both xml files present in two different folders have same name.
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
I have two layout files, one for landscape (layout-land) and one for portrait (layout-port). When I run my application the correct layout is grabbed initially dependent upon how I am holding my phone. If I am holding landscape, landscape is grabbed and visa versa. BUT, once I initially launch my application, if I change orientation the first layout grabbed at launch is used when switching orientation.
How do I get Android to changed layout xml per orientation change after initial launch of applicaiton?
In my manifest I have android:configChanges="orientation/keyboardHidden"
When you specify configChanges in the manifest for an activity, the activity has to handle the events by itself. Either do the required layout change in the activity or remove the configChanges in the manifest.
http://developer.android.com/guide/topics/manifest/activity-element.html#config