Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Level onResume Android

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.

like image 457
Aritra Roy Avatar asked Feb 24 '15 09:02

Aritra Roy


People also ask

What is onResume in Android?

onResume() is one of the methods called throughout the activity lifecycle. onResume() is the counterpart to onPause() which is called anytime an activity is hidden from view, e.g. if you start a new activity that hides it. onResume() is called when the activity that was hidden comes back to view on the screen.

Is onResume called before onCreate?

onResume() will never be called before onCreate() . Save this answer. Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .

What is Android application lifecycle?

An Android application has a lifecycle. It crosses through various stages from when a user opens and exits an application. An application's state helps you manage when a user opens an activity, pauses, resumes, stops, and destroys it. Callback methods manage these states.

What is visible activity in Android?

A process is considered visible in the following conditions: It is running an Activity that is visible to the user on-screen but not in the foreground (its onPause() method has been called).


2 Answers

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) {

    }
}
like image 54
Shahab Rauf Avatar answered Sep 25 '22 12:09

Shahab Rauf


Try below sample

    /**
 * TODO : After update to API level 14 (Android 4.0),
 * We should implement Application.ActivityLifecycleCallbacks
 */
public class GlobalApplication extends android.app.Application
{
    private boolean inForeground = true;
    private int resumed = 0;
    private int paused = 0;

    public void onActivityResumed( Activity activity )
    {
        ++resumed;

        if( !inForeground )
        {
            // Don't check for foreground or background right away
            // finishing an activity and starting a new one will trigger to many
            // foreground <---> background switches
            //
            // In half a second call foregroundOrBackground
        }
    }

    public void onActivityPaused( Activity activity )
    {
        ++paused;

        if( inForeground )
        {
            // Don't check for foreground or background right away
            // finishing an activity and starting a new one will trigger to many
            // foreground <---> background switches
            //
            // In half a second call foregroundOrBackground
        }
    }

    public void foregroundOrBackground()
    {
        if( paused >= resumed && inForeground )
        {
            inForeground = false;
        }
        else if( resumed > paused && !inForeground )
        {
            inForeground = true;
        }
    }
}

Put below code in your all activities.

  public class BaseActivity extends android.app.Activity
{
    private GlobalApplication globalApplication;

    @Override
    protected void onCreate()
    {
        globalApplication = (GlobalApplication) getApplication();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        globalApplication.onActivityResumed(this);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        globalApplication.onActivityPaused(this);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}
like image 36
sagar Avatar answered Sep 26 '22 12:09

sagar