Android - Slider devices cause Activities to reset - android

While testing on Android 1.6 using a G1 I have noticed that when I slide out the keyboard it kills the activity and recreates it even though I have set my activity to only display in portrait mode.
Same happens when I push the keyboard back in.
I get onSaveInstance, onDestroy called, then onCreate, onResume, OnrestoreInstance...
I understand why this is done when the display is being switched to landscape view but why does this happen when I specifically dont want my activity to switch view, its essentially killing and restarting the activity for no reason.
Is it the same on 2.x devices?
Is there something I'm missing to stop it happening?
Can anyone explain if there is any point to it?

This is the recommended behavior in Android. But if you want to stop this you can specify the following property in your Manifest against your Activity
android:configChanges="keyboardHidden|orientation"

Related

Simulate backstack savedInstanceState behaviour

I want to test the state of my app when it's left in the backstack for too long.
When I open like 20 apps one after the other my last used app (which is the app I'm testing) eventually terminates and a savedInstanceState occurs. Is there a way to simulate this behaviour for the app I'm testing without having to open another 20 apps in order to burry my app in backstack?
Yes, there's a simple way to do it. Just enable Do not keep activities under Developer options. Your activity will then be immediately destroyed as soon you leave it and onRestoreInstanceState() will be invoked when you return.
Alternatively, you can force a configuration change (like orientation) and that will recreate your activity too. One issue with this approach is that there's a bug with some JellyBean/KitKat versions where the emulators fail to rotate. (This works fine on a device though.)
The workaround is to install the RotateScreenOrientation.apk which can force the emulator to be in portrait or landscape but this quickly becomes tedious if you have the option to Wipe user data on emulator start enabled.

Why when rotating the phone from portrait into landscape, does the app lose all data?

I'm trying to create an app in Android, using Eclipse ADT and I use a HTC phone. Here's the problem: after running the app on the phone from Eclipse, I add elements to my ListView and everything is fine, BUT when I turn the device in landscape mode the hole app seems like it restart, there's no more records inside. It's like new. Any ideas why and how I can solve this problem? Please don't tell me to deactivate the screen rotate option from phone settings.
This is the way android handles orientation changes. It reloads your whole activity. The normal way to handle this situation is to save the state of your activity in onPause() and then retrieve it back in onCreate().
Here is more information on the android activity lifecycle:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
EDIT:
You should implement onPause() anyway, because it will also be called if the phone rings in the middle of running your activity. In this case, when the user comes back from the call your activity will possibly be reloaded from scratch again and the user will lose their state.
There is a similar question here.
Basically you application is restarted. You can either force your application to portrait or follow the steps here to find out how to handle it properly.
I made answer over here about this sort of thing.
Please don't tell me to deactivate the screen rotate option from phone settings.
I'm not sure if you meant the following, but here's a solution:
Add android:configChanges="orientation|screenSize" to your <activity tag, which is in your AndroidManifest.xml, like so:
<activity
android:name="activity_name"
android:label="#string/app_name"
android:configChanges="orientation|screenSize" />
This will prevent the activity from being destroyed when the orientation is changed, like it usually would. There are other ways to fix this as well. Please leave a comment if this is not your desired solution. I can make up another.

Weird orientation changes on Honeycomb between activities

I have an app which has an activity A, that lets the user click on a thumbnail and go to another activity B which shows the thumbnail full screen. On activity B, if the user clicks anywhere on the screen, it closes the activity.
Both activities are defined as portrait in the manifest, and both have the onConfigChange value to contain the orientation flag.
It works perfectly on all versions of Android and all devices that I've tested, except one - Motorola Xoom, with Android 3.1. On the Xoom, if the user navigates quickly between the activities (back and forth), it has a chance to show activity A in landscape mode for a very short time, as if it planned to switch to it.
Not only that, but if it remove the flag of the onConfigChange in the manifest, activity A might re-create itself from scratch on this special case.
What could cause this weird thing? Is it some weird bug on android 3.1 or the Xoom? Is there anyway i can fix this issue? I could have something that blocks the touch on activity B for a few ms on the beginning, but that's just a workaround.

Android application orientation change behavior

I'm done with developing an android application and in phase of testing it. I've tested it with the simulator and everything is working perfect, but when I deploy it on devices, some of them when you change the screen orientation the application screen starts to flicker.
Any clue where to start the problem investigation from?
When orientation changes from portrait to landscape or vice versa, onCreate gets called again, making the application to start from the beginning. Make sure you are handling it in manifest by declaring android:configChanges="keyboardHidden|orientation" infront of your class name or override onConfigChanged() function.
Please check Both Device version and Developing Application Versions are the same or not, and clear the application after install to the device and then run.

Saving view states with Honeycomb

I developed a pretty simple game that uses a custom view for drawing to the screen. On my phone (Android 2.2), I can press the home button on the device and do other tasks. When returning to the game, it is restored to the exact state that it was before. The thing is, I didn't have to override any methods or really do anything for this behavior to occur. However, on Honeycomb, it resets everything like I would expect.
It's very puzzling... I was wondering if there was a way to make Honeycomb behave like 2.2 in this regard. I'm not too familiar with saving view states, but since I have a LOT of variables (hundreds, depending on how custom objects are saved), I imagine it being unpleasant to manually do.
I was able to solve it. Kind of.
Basically, the problem was caused by the way that Android handles the screen being fixed to portrait mode in my activity. In 2.2, Android would open the activity in portrait mode and not restart the activity. However, in Android 3.0, it opens the activity, and THEN rotates the screen, causing it to restart the activity (since that happens when a rotate happens).
To fix this, I added the following to my activity in the manifest:
android:configChanges="keyboardHidden|orientation"
This tells Android that you will handle config changes yourself. In my case, I do nothing, since my application is locked in portrait mode.
The reason the state isn't cleared when the application is paused is because onCreate() doesn't get called. I am aware that Android can kill the application though, which would call onCreate(), so I will still have to handle that situation by saving the Activity variables and recreating the View with them.

Categories

Resources