views:

85

answers:

2

With the Android 2.2 update came a new car dock app. If my app is in the foreground when I dock my phone, my app gets killed or paused or something, and when I try to re-open my app, it's all messed up. It has lost its state information.

Does anybody know what the car dock app is doing? At very least, it seems to be force killing all running applications or some other destructive behavior.

I guess once we identify what it is doing, I'll be able to go about coding around it.

+1  A: 

All apps lose their state when they're killed. In fact, they lose their information when you change the orientation, unless you specifically save it (say, in onSaveInstanceState).

If you want your information to be so persistent that it can survive an app switch (or system reset), you can save your state in the SharedPreferences instead of the bundle you get in onSaveInstanceState.

How do you currently save your state? Do you do that at all?

Btw, you can also add a broadcast receiver to listen for ACTION_DOCK_EVENT and check the state for EXTRA_DOCK_STATE_CAR and save your instance data, in case you don't like the general SharedPreferences idea.

EboMike
Update: The system first is calling my `onRetainNonConfigurationInstance` method, followed immediately by `onDestroy`. So I'm saving my state, then onDestroy fires of a chain of destruction, then when its time to restore my state, the retained instance is dead because onDestroy was called and so my app ran its shutdown sequence. I'm maintaining Bluetooth states, so how am I supposed to know if onDestroy is really a destroy or just a "fake/temporary" destroy?
Brad Hein
How does your app handle a change of orientation? Switching from portrait to landscape will also result in onDestroy, followed immediately by onCreate. There is an `Activity.isFinishing()` that tells you just that. Check out the documentation for Activity.
EboMike
I used `android:configChanges` to block orientation change destruction.
Brad Hein
I guess that's why you never noticed before that your app doesn't handle the app recreation right. You might even have the same problem if you switch to a different app and then switch back to yours (say, by holding the HOME button).
EboMike
A: 

Going into a car dock is a configuration change (so apps can present a car-specific UI if they want to, for example). This means your activity will go through its normal process of being destroyed and later recreated.

If you are abusing android:configChanges to not have to deal with this, then the answer is to NOT do this. As we've always said, this is there for certain specific cases where you want that optimization. It is not an excuse to avoid dealing with configuration changes. If your app doesn't deal correctly with configuration changes, there will always be various ways it can be broken. The new car dock feature is just making that a little more obvious.

In fact I very much recommend that developers do not use android:configChanges. Keeping the normal behavior is a very good way to test this part of your app to ensure that you are doing it correctly, and will behave correctly when it happens to you for other reasons (because it most certainly will). Turning off config changes for the orientation switch makes it a lot harder to test this part of your app. Except for very unusual situations, you really should keep the normal behavior here and make it work correctly.

hackbod