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
Related
I have made a simple dice game that makes use of the setTranslationY() method to indicate if a dice should be ignored for the next dice roll. This all works fine, except that when I try to restore the state of the app after a screen rotation, my restoring method cannot seem to use setTranslationY() on the views that need it.
I have confirmed time and time again the the method IS being called for all views that should be nudged upwards a little bit, but the views stay where they are.
If it is so that the app needs to go through the process of completing onCreate(), onStart(), onRestoreInstanceState() and onResume() before being able to manipulate views then please advise on how to achieve this. I need this to happen consistently upon every device rotation.
You can retain the state of your application by informing Android you'd like to handle the logic for screen orientation yourself, rather than allowing the OS to trash and rebuild the entire Activity whenever the user tilts the screen. You can do this by adding the android:configChanges attribute to the Activity entry in your AndroidManifest.xml:
<activity android:name=".YourActivity"
android:configChanges="orientation"
android:label="#string/app_name">
This should hopefully preserve the state of your animated Views.
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 would like my Android app to instantiate it's home screen activity one time only. I am managing the back stack appropriately to achieve this but have just discovered an orientation issue when the app starts up.
Visually this orientation change only shows itself on the emulator. (probably runs too fast to be observed on a device).
Here's what happens :: -->
activity.onCreate()
activity.onDestroy()
activity.onCreate()
This sequence makes sense and is caused by the change in orientation. What does not make sense (to me) is that it happens at all because I have done the following to prevent an orientation change :: -->
AndroidManifest.xml contains
android:screenOrientation="portrait"
for all my activities and in the home screen activity onCreate() method, I'm calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
So why do I get an orientation change?
Add android:screenOrientation="portrait" in your manifest file where you declare your activity like this
<activity android:name=".yourActivity"
....
android:screenOrientation="portrait"/>
if you want to do using java code
try
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
before you setContentView for your activity in onCreate()
see here
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.
I have a canvas, and it is redrawn when orientation is changed. It's a custom canvas not android provided.
When screen layout is changed, application state and (all of it's view's states) gets reset. I changed screen orientation to portrait only; it keeps screen layout unchanged but application is again resetted.
I checked documentation and found that activity is destroyed and restarted again when orientation change occurs. Savestate() cannot save and load bitmap data or any large data which are required by my custom canvas.
I again checked documentation, and found Handling run time changes topic, which mentioned onConfigurationChanged() which gets called when specific configuration change is occurred, which in my case is 'orientation'. This method prevents restart and leaves to developer how that configuration change should be implemented. It even mentioned in last paragraph that if I will not implement that method then this will just cause the activity to skip onRestart() and will do nothing. I am setting manifest file as
android:screenOrientation="portrait"
android:configChanges="orientation"
And I am not implementing onConfigurationChanged(). But this doesn't help either. I don't know why. It seemed so helpful to me.
Please post solution if you have any.
Also, app takes some resonable time, and I would like app do not restart when orientation is changed. Actually I don't want to do anything when this happens.
I am using emulator too, so please clarify if it is emulator only problem.
P.S. My internet connection is down and I am using my stupid mobile client.
I have checked the offline documentation. And please bear with me for spellings. I am trying to find solution, but currently I am stumpped.
Use this in you AndroidMenifest.xml
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboard|keyboardHidden"
android:screenOrientation="sensor" />
write this code in manifest:
android:configChanges="orientation|screenSize|keyboardHidden"
For each activity in the AndroidManifest, the screenOrientation can be specified. For example, to specify that the activity always stays in portrait mode, the following can be
added to the activity element:
android:screenOrientation="portrait"
Similarly, landscape mode can be specified using the following:
android:screenOrientation="landscape"
However, the previous still causes the activity to be destroyed and restarted when a hard
keyboard is slid out.Therefore, a third method is possible:Tell the Android system that the
application should handle orientation and keyboard slide-out events.This is done by
adding the following attribute to the activity element:
android:configChanges="orientation|keyboardHidden"