0

I have a problem with my application when it is brought back to the foreground in the case the phone went low on memory while the application was hidden:

The class inheriting from Application is re-created (onCreate is called again), thus losing data it held before. The Activity which is restarted is not the one tagged as main action in the Manifest, but the last one that was active. This is a problem as the main activity, from which the user logs in, is the one responsible for filling in the Application subclass' data and I can't fill it in later.

Is there any way to tell the application to restart at the main activity instead of the latest one in this case ?

Jukurrpa
  • 4,038
  • 7
  • 43
  • 73

2 Answers2

1

You could check in onResume() if the user is logged in. Means that you check if your Application data is filled. If this is not the case, finish the activity and start your first Application.

The user expect to return to the latest activity, so a general "always start first activity" would upset the user...

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • I quickly tried to check this in the Activity's `onCreate()` method, but calling `finish()` from there makes the app crash. So I wondered it there was a cleaner way to do this. – Jukurrpa Jul 03 '13 at 15:08
  • I just saw [this article](http://www.truiton.com/2013/03/android-force-close-application-session-timeout/). Maybe you can adopt a bit. He closes every activity when the app is minimized. I highly recommend to outsource the "fill data" procedure to let it happen everywhere it is needed. Store the login data, login even when not going through the first activity... – WarrenFaith Jul 03 '13 at 15:15
  • Actually I just tried to do what you suggested in the Activity's `onResume()` method instead and it worked. I just call `finish()` if it detects the `Application` subclass data is not initialized. I'll mark your answer as the good one, just change `onCreate()` to `onResume()` – Jukurrpa Jul 03 '13 at 15:20
0

you could keep the data by implementing methods such as onPause() and onResume() Check this link out: http://developer.android.com/reference/android/app/Activity.html

this answer might also be helpful: onSaveInstanceState () and onRestoreInstanceState ()

Community
  • 1
  • 1
j l
  • 61
  • 1
  • 2
  • 11
  • The data I wish to retain is stored in the `Application` subclass, unfortunately... I'm not sure storing its data in an Activity's saved bundle is the proper thing to do. – Jukurrpa Jul 03 '13 at 15:17