Application Level onResume Android

放肆的年华 提交于 2019-12-05 06:03:20

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

    }
}

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();
    }
}
LaurentY

You could try with this flag FLAG_ACTIVITY_LAUNCHER_FROM _HISTORY:

if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY )!=0){
    Log.d(TAG, "Called from history");
    //clear flag from history
    Intent intent = getIntent().setFlags( getIntent().getFlags() & (~ Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY));
    setIntent(intent);
}

Source : Android - detecting application launch from home or history

When "A" Activity is start from recent, this flag is present. Now this flag will be also present if "A" activity call "B" activity and on "B" user press back. So you have to check flag and when you detect it you have clear intent by removing this flag, source: Remove a Paint Flag in Android

I would suggest using LifecycleObserver. If your Application class implements this interface it marks a class as a LifecycleObserver, it does not have any methods, instead, it relies on OnLifecycleEvent annotated methods. The usage is simple:

public class AndroidApplication extends Application implements LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppStart() {
         //enter code here
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppStop() {
         //enter code here
    }

    ...etc
}

With Lifecycle.Event you can access all lifecycle states through Enum. It is part of androidx.

Try this

import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks   {

    private Activity mLastActivity;

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);

         mLastActivity = null;
    }

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

    @Override 
    public void onActivityStarted(Activity activity) { }

    @Override public void onActivityResumed(Activity activity) {
        if(mLastActivity != null && mLastActivity.equals(activity)) {
             // Your function 
        }
        mLastActivity = activity;
    }

    @Override 
    public void onActivityPaused(Activity activity) { }

    @Override 
    public void onActivityStopped(Activity activity) { }

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

    @Override 
    public void onActivityDestroyed(Activity activity) { }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!