-1

On start my app displays a splash screen and checks via network if the current user is still premium.

My problem: I started my app right before I went to bed and minimized it by pressing the home button. In the morning I launched the app again and it resumed the activity from the night. The app never really quit, my splash screen was not shown and and it couldn't check if the user is still premium.

So how can I achieve my app to be closed after a certain time (e.g. when the app is minimized)?

Chris
  • 4,255
  • 7
  • 42
  • 83
  • 1
    Please don't bother to close the app, let the system handle it. Move your user premium check to [onResume](http://developer.android.com/reference/android/app/Activity.html#onResume%28%29) method. – Mudassir Jun 05 '14 at 06:43

4 Answers4

2

You should write the Premium user check logic in your onResume() method so that if the activity is in pause or background state it will check the logic every time it will be launched .

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
1

Don't try to finish app when it's minimized. Use Activity lifecycle callbacks.

@Override
protected void onResume (){
   //check for changes here
}
  • There's a variety of good reasons to end activities when they've been minimized for a while. For example, a mobile banking app should automatically log me out after a set amount of time. Really anything that connects to a password controlled service should. – Gabe Sechan Jun 05 '14 at 06:43
  • 1
    @GabeSechan Using finish() isn't a graceful approach for that. You can achieve the same with using lifecycle callbacks and logging the user out in onPause(); –  Jun 05 '14 at 06:49
  • Depending on the architecture of the app, that may or may not be the case. For example, if your app has a single LoginActivity that acts as a gatekeeper to the rest of your app, there is no way to just log you out without finishing that activity. Impossible to say without code. – Gabe Sechan Jun 05 '14 at 07:36
0

If you want to end an activity, you call finish(). So you could record the time in onPause, then in onResume, check how long its been. If its been too long, call startActivity on your main activity, then call finish() to end the old one.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

I think you should become more familiar with the Android Activity Lifecycle and think about which call back in your activity should you check if the user is premium

Onik
  • 19,396
  • 14
  • 68
  • 91