5

Does anybody know some event listener for detecting when the application comes back to the foreground from the background? I searched a lot, but I didn't find such a listener. Any other solution is appreciated. Thank you!

strongmayer
  • 488
  • 1
  • 5
  • 14

4 Answers4

3

I does something like this, in a BaseActivity

in onResume I check enterCount is equals 0 and then add the enterCount in onPause I delay the enterCount to decrease in 300ms, and it seems good when the onCreate isn't delay two long, or we can do something in onCreate to avoid the first time.

protected void onResume(){
         int  enterCount=GlobalManager.getInstance().getEnterCount();
        if(enterCount==0){
            //do some thing for first enter 
        }
        GlobalManager.getInstance().increaseEnterCount();
}

protected void onPause(){
        GlobalManager.getInstance().decreaseEnterCountDelay();

}
powerfj
  • 156
  • 6
3

You may need to be more specific about your use case.

Check out the Activity Lifecycle.

Both onResume(), and onStart() will get called when your activity comes to the foreground.

Edit: onRestart()?

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • I need exactly that event, when the application comes from the background into the foreground. It's true that onResume() and onStart() are called in this case, but not only in this case. That's the problem. How can I make a difference between the cases? How can I say if the application is coming back from the background to the foreground or if the activity was simply restarted? – strongmayer Mar 22 '12 at 13:49
2

I'm not exactly sure what you are trying to do, but if you want to tell the difference between these 2 sequences:

  • onCreate(), onStart(), onResume()
  • onPause(), onResume()

you could just set a flag in onPause() and check that flag in onResume().

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

From Android 19, you can register an app life cycle callback in your Application class's onCreate() like this:

@Override
public void onCreate() {
    super.onCreate();
    registerActivityLifecycleCallbacks(new AppLifecycleCallback());
}

The AppLifecycleCallback looks like this:

class AppLifecycleCallback implements Application.ActivityLifecycleCallbacks {
    private int numStarted = 0;

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (numStarted == 0) {
           //app went to foreground
        }
        numStarted++;
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        numStarted--;
        if (numStarted == 0) {
            // app went to background
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}
Ziwei Zeng
  • 691
  • 5
  • 21