0

On Android, I'm trying to catch the event where my app comes back to the foreground from the background. In other words, the app was active, then the user minimized it (by clicking the home button or starting another app or something), and then the user runs the app again.

I can add the onRestart() method to an activity as follows:

@Override
public void onRestart() {
    super.onRestart();

    Log.d("MAIN", "onRestart called.");
}

But this only works when that specific activity is the active one when the user minimizes the app.

Is there a way to catch this for the whole app somehow? Or do I need to add onRestart to every single activity I have? (I suppose I could create a superclass on which all the other activities are based).

Thanks!

Nico teWinkel
  • 870
  • 11
  • 19

2 Answers2

2

Is there a way to catch this for the whole app somehow?

No, because there is no concept of an app "restarting".

Or do I need to add onRestart to every single activity I have?

Presumably. Or, find a way to avoid needing to "catch the event where [your] app comes back to the foreground from the background".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the confirmation! – Nico teWinkel Jul 09 '13 at 20:31
  • The reason I want it is to check if a user session has expired on the server, which is something that applies to the whole app. I'll just add the same check to the other views. – Nico teWinkel Jul 09 '13 at 20:38
  • @NicoteWinkel: IMHO, you "check if a user session has expired on the server" on the basis of time, not whether an app has been in the foreground, background, sideground, underground, or overground. – CommonsWare Jul 09 '13 at 20:56
  • Thanks @CommonsWare, that makes sense. Our case is a bit more unique - the "session" is more of a user account being active, and the app is not used by the user very often. We noticed that users were not exiting applications, so they would leave it minimized for days or weeks and then bring it to foreground again. I think if I just handle the main views, it should work well enough. – Nico teWinkel Jul 09 '13 at 21:04
0

I think the method you need is void onResume()

here is there android developers page for activities , check the "Implementing the lifecycle callbacks" part of the page .
http://developer.android.com/guide/components/activities.html

hope this helps.

DrkStr
  • 1,752
  • 5
  • 38
  • 90
  • Thanks for the help. I saw that link (and the Activity LifeCycle) on a related question. I did try onResume() before posting this question, but it is also activity-centric, and also didn't do the trick. edit: onResume is called every single time I show that view, which is not the desired effect. – Nico teWinkel Jul 09 '13 at 20:29