1

I have an Android application. When I tap the home button and ‘minimise’ the app, I want to be able to execute code when the user reopens it.

Looking at methods such as onResume() and onCreate() I think they don't fit my needs.

I specifically want to prompt the user with a login dialog before he continues where he left off.

Any suggestions?

manfcas
  • 1,933
  • 7
  • 28
  • 47
panthor314
  • 318
  • 1
  • 14
  • Possible duplicate (I posted a couple of answers there, try them): http://stackoverflow.com/questions/38670874/what-are-the-android-application-lifecycle-methods-not-activity-life-cycle-met/38671717#38671717 – Shaishav Aug 05 '16 at 19:21
  • 1
    Why "onResume()" and "onCreate()" don't fit your needs? You can use them in combination with another Activity (like your "LoginActivity") and then send a result to the original Activity in order to make a decision (like when login failed or success). – josemgu91 Aug 05 '16 at 19:22
  • @josemgu91 how would i know if the app is coming back from sleep or not? – panthor314 Aug 05 '16 at 19:25
  • @panthor314 Like I mentioned, check the question in my comment above, it covers that. – Shaishav Aug 05 '16 at 19:26
  • @Shaishav trying it out now – panthor314 Aug 05 '16 at 19:43
  • @Shaishav I just implemented `OnActivityResume()` and it never gets called? – panthor314 Aug 05 '16 at 19:46
  • @panthor314 in the application class? Is the application class registered in the manifest? – Shaishav Aug 05 '16 at 19:47

1 Answers1

0

Overriding onRestart() will do the job! It is only called when a previous onStop() was called.

However on some android platforms onPause() only get's called when you press home so a trick would be to call super.onStop() within onPause() and then you can ensure that onRestart() will get called.

Moshe Rabaev
  • 1,892
  • 16
  • 31
  • `onRestart()` gets called when the `Activity` comes back into the foreground, meaning it would get called each time you return to the activity (whether it be from outside the app, or within the app itself (i.e. press back button)) – panthor314 Aug 05 '16 at 19:35
  • isn't that what you want? – Moshe Rabaev Aug 05 '16 at 19:37
  • check this post look at my answer it could be your problem: http://stackoverflow.com/questions/8881951/detect-home-button-press-in-android/31735364#31735364 – Moshe Rabaev Aug 05 '16 at 19:38
  • i don't want to prompt the user to login everytime they hit back button – panthor314 Aug 05 '16 at 19:38