Prevent app from restarting during orientation change - android

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"

Related

data lose and force close in landscape and portrait mode android

I am developing an android application I am facing below two problems,
data lose : login screen when I change the landscape to portrait mode the entered text will be lost.
force close: Inside the application when I captured the image form camera then I change the portrait to landscape mode some time getting force close and image not showing in ImageView.
How to solve this problem? I should not lock the application in portrait mode and I have no idea to solve this please suggest me any useful links and sample code it might be useful for me.
add below property in your mainifest file inside the activity.you can change as per your requirement
android:configChanges="orientation|screenSize|keyboard|screenLayout"
In order to prevent data loss, add configChanges to the manifest, and to it set the values: screenOrientation, screenSize, layoutDirections.
and if that too does not work,
override the onConfigurationChanged method, and handle what you have to do with the data in that method.
1)
android:configChanges="orientation|screenSize|keyboard|screenLayout"
add this to you AndroidManifest.xml file for which makes sure onCreate() is not called again when orientation change happens, but only the ui rendered will change maintaining the user entered data on UI.
<activity
android:name="LoginActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:logo="#drawable/menu_button"
android:windowSoftInputMode="adjustPan|stateHidden" >
2)
need more info, however this might be case of when orientation gets changed ui views will be recreated hence will be null for some very small fraction of time and your code might be trying to access the same view in this time.
I feel 1) and 2) are related, solve 1) using above and try to reproduce 2) again.

Can a live wallpaper lock the screen in portrait mode?

Can a live wallpaper lock the screen in portrait mode? And if so, how?
I should mention that I have seen two supposed answers to this question on SO, one appeared inordinately complicated, I didn't understand it all and the answer was not accepted by the original poster. The second answer didn't work for me.
A third answer involving the use of:
android:screenOrientatin = "portrait" or "landscape"
has been suggested, but it is not clear exactly where this should go in the manifest.
EDIT: have tried putting android:screenOrientation="portrait" in many different places in the manifest, and none of them worked.
EDIT: another answer was to rotate your bitmaps and handle a rotation by just drawing everything sideways - but this looks very ugly because, as you rotate you phone, the OS instigates a rotation animation - which means that you get a horrid jumping effect as you turn the phone.
I'm beginning to suspect that the true answer is simply "no".
Did you try setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ?
In your AndroidManifest.xml you should have something like:
<application
android:icon="#drawable/app_icon"
android:label="#string/nuboLogin"
android:name=".LoginApplication"
android:debuggable="true">
<activity
android:name=".WallPaperActivity"
android:label="#string/wallPaper"
android:windowSoftInputMode="adjustPan"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
This should make sure that your Activity runs in portrait mode. If you prefer landscape, you can easily guess what you should modify
Android application restarts the activity when the orientation changes. You can either use
android:configChanges in your manifest. The activity is shut down and restarted by default, when a configuration change occurs at runtime, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
use android:screenOrientatin = "portrait" or "landscape" it will force the app to run in the mode you specify. However it will not prevent the activity from being shut down and restarted.

Android Lock Orientation and Double Create

I want to make my application displayed only in portrait orientation, so I have put android:screenOrientation="portrait" in the Activity tag in AndroidManifest.xml, and have put setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in the Activity's onCreate method.
This works to lock the orientation to portrait, however, when the Activity starts, it shows itself once, then shows itself again, so you see a sort of flash. I can confirm that onCreate is being called twice as well.
This flash is causing further force closes later in my application.
How can I eliminate this flash?
Edit
I had a splash screen displaying before the activity I had described. It was being run twice, and therefore ran this activity twice (via an Intent) twice. I fixed this issue by checking if this activity had already been stared using an intent in the splash screen class, and it had, not to run it again. The fix was more of a workaround than a fix, but I hope it helps people.
I believe that using the Activity's configChanges attribute (in the manifest) should solve your problem. ConfigChanges attribute documentation
However, you are just going around another problem. What you really should address is why is that provoking FC? There is absolutely no reason for that to happen so you also should solve that problem. More info in handling runtime changes.
Please note: using the first approach is acceptable to address the performance/UX issues though.
Just in case you wonder why onCreate is called twice, once I got into this problem and I think that it was related to having the orientation fixed and then having another activity being created but in a different orientation. Before the second activity was started, the former activity changed into the orientation of the latter. And remember that keyguard is also an Activity! I'm not sure if this happens for this reason though.
don't put : "setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);".
Just let the AndroidManifest do his job. I thing what you did is :
Tell your App to go only in Portrait mode
Tell your Activity to be in Portrait mode, wherever it was already like this or not (The flash have to come from this).
I could be wrong.

Android options menu closed every time screen orientation changes

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

can i allow an android activity to change to landscape only on certain conditions?

most of my application will not look so well on landscape, but that's the user choice as far as i'm concerned if to see the application on 'wide' screen.
However i have an auto complete screen and that screen cannot function in landscape mode since when user input text and i bring the results into the list view on screen the user can't see the listview since the virtual keyboard covers it all.
So i thought of defining in the manifest that this activity wont be used in landscape, but i CAN use it in landscape mode for devices with hardware keyboard that is open...
So either i can define it in the manifest (couldn't find how) or i need to do it melodramatically (discover the keyboard existence and open to support it, or disable the rotation otherwise).
Anyone knows how to do that ?:
You can try
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
OR
activity android:name=".SomeActivity"
android:screenOrientation="portrait"
these may help you.
you can also intercept these config changes:
<activity ...
android:configChanges="orientation|keyboardHidden">
</activity>
and in onConfigurationChanged() you can decide what to do with them.

Categories

Resources