Application Level onResume Android

Aritra Roy picture Aritra Roy · Feb 24, 2015 · Viewed 9.6k times · Source

Problem

The idea is very simple. Whenever an user comes back to my app from the Recents I want to show a simple dialog prompting with the password.

I know how to prompt the dialog with password, but my problem is how do I understand that the user has entered my app from the recents. If I put the prompt in the onResume in every activity, then it will get triggered everytime even if the user doesn't enter from the Recents menu.

There are lots of activities and fragments in my app. So, I would love to have a more generic or application level solution.

Answer

Shahab Rauf picture Shahab Rauf · Jan 4, 2016

Implement Application.ActivityLifecycleCallbacks, that will provide all activity callback in your application class.


public class AppController extends Application implements  
Application.ActivityLifecycleCallbacks  
{   
    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);

    }


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

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

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

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}