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.
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 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 a lot ot async tasks in my activity. If screen rotation occurs - they gone. What to do to prevent asynctasks from trowing exception?
I think exception is due to the re starting of the activity in orientation changes, if use android:configChanges="orientation|keyboardHidden" for your activity in your manifest, which prevents restart
use
android:configChanges="keyboardHidden|orientation"
as a attribute of your Activity in AndroidManifest.xml file to prevent reload Activity on screen rotation .
The most proper way to this is to use a fragment to retain the instance of the async task, over rotations.
Here is a link to very simple example making it easy to follow and integrate this technique into your apps. This works brilliantly all buttons and images etc are redrawn as expected
https://gist.github.com/daichan4649/2480065
For some reason, every time the screen orientation is changed my app closes the context menu if it was open. All applications I have seen keep menus open after rotating the screen, but I cannot figure out why my app is closing them.
I am not handling configuration changes on my own and my onCreate method, which I know is called after every orientation change, does not touch menus at all. I would appreciate any insights on this problem.
The contextMenu disappeared because by default when rotating android calls destroy() and then onCreate() but :
If you don't want Android to go through the normal activity destroy-and-recreate process; instead, you want to handle recreating the views yourself, you can use the android:configChanges attributes on the element in AndroidManifest.xml.
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden">
</activity>
This way contextMenu is not closed when phone rotates, because onCreate() method is not called.
This topic can also be helpful - Activity restart on rotation Android