3

I'm wondering why the Motorola Milestone with 2.1-update1 behaves differently from the Emulator or e.g. the Nexus One. I am trying to exit my app with:

@Override
protected void onPause() {
    if(mayDestroyActivity) this.finish();
    super.onPause();
}

This works well on either Emulator or Nexus One. onDestroy() gets called immediatly after onPause() and onStop. But not for the Milestone. Instead, onDestroy() gets called when another Activity is started. Its section in the Manifest looks like this:

<activity android:name=".MyActivity"  
    android:configChanges="orientation|keyboardHidden"  
 android:label="@string/questionnaire_item"
 android:launchMode="singleInstance"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
 android:windowSoftInputMode="adjustPan">
 <intent-filter>
  <category android:name="android.intent.category.OPENABLE" />
 </intent-filter>
</activity>

Does anyone have a hint on this? My app depends on exiting properly since I save all progress in onDestroy()

Thanks,
Steff

stfn
  • 1,140
  • 5
  • 19
  • 33

3 Answers3

5

You are doing it wrong. This is what the Doc says:

Note: do not count on this method being called as a place for saving data! [...] There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
  • 1
    You're right, my approach is wrong. But anyhow, I want my app to pass onDestroy() when the user exits to the home screen. – stfn Jun 14 '10 at 10:12
  • There is no way to do the wrong approach right. "There are situations where the system will simply kill the activity's hosting process without calling this method" is a statement you should take literally. – Erich Kitzmueller Jun 14 '10 at 11:41
  • 1
    You're right again. I moved everything to onPause(). Works as well. Thanks – stfn Jun 15 '10 at 08:42
  • @stfn thanks for mentioning onPause(). I also had this issue and moved finish() from onStop() to onPause() and now onDestroy() is called. – MartinC Nov 28 '13 at 09:20
2

You should use onSaveInstanceState, check this link.

Community
  • 1
  • 1
Macarse
  • 91,829
  • 44
  • 175
  • 230
2

If the user exits to the home screen this does not need to mean that the app should be exited. In most of the time it will give a better user experience if the app just goes to the background. Try to rely on saveInstanceState and onPause.

Exiting a app is seen as bad practice in the android world, featuring a phone with a good system for true multitouch can get better user experience if the app keeps running after pressing the home screen.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • Thanks, I figured it out. The Milestone's different behavior made me finally see things the right way. Strange but funny and useful... – stfn Jun 15 '10 at 08:44