从后台返回 Activity 时执行某些操作

Doing something when returning to an Activity from Background

我只想在从后台返回到应用程序时才执行一个功能。

我在onResume中包含了方法,这在一定程度上做到了。问题是因为即使在创建 Activity 和从另一个 activity 返回到 activity 时 onResume 也会被触发(例如:从按下后退按钮),并且功能是也在那里执行。

如何避免这种情况并仅在从后台返回时才执行函数?

Ps:我的应用程序已经有多个地方使用 startActivity,因此更改为 startActivityForResult 是一项乏味的任务。

此外,我所有的 Activity 都是从一个公共 BaseAppCompactActivity class 扩展而来的,它是我的方法所在的位置,因此这将适用于整个应用程序。

编辑 2: 我的 BaseAppCompactActivity 如下所示,现在已实施 LifecycleObserver。但这似乎不起作用。

public class BaseAppCompactActivity extends AppCompatActivity implements LifecycleObserver {
    private String TAG = BaseAppCompactActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopService();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        startService();
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
////        updateLastAccessedDate();
//    }

    private void startService() {
        startService(new Intent(this, BusinessCacheService.class));
    }

    private void stopService() {
        stopService(new Intent(this, BusinessCacheService.class));
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private void updateLastAccessedDate() {
        //Do something
    }
}

您可以使用 startActivityForResult 而不是 startActivity。 然后你可以在onActivityResult方法中捕获返回。

首先像这样设置一个全局布尔变量:-

boolean isPaused = false; 

现在在您的 activity 中设置一个方法 :-

@Override
    protected void onUserLeaveHint() {
        isPaused = true;
        super.onUserLeaveHint();
    }

或在您的 onResume 方法中:-

@Override
    protected void onResume() {
        if(isPaused){
         isPaused = false;

          }
super.onResume();
    }

像这样

将这些变量添加到您的主 activity

public static boolean isAppWentToBg = true;
public static boolean isWindowFocused = false;
public static boolean isBackPressed = false;

并添加这些方法

private void applicationWillEnterForeground() {
        if (isAppWentToBg) {
            isAppWentToBg = false;
//            Toast.makeText(getApplicationContext(), "App is in foreground", Toast.LENGTH_SHORT).show();
        }
    }

    public void applicationdidenterbackground() {
        if (!isWindowFocused) {
            isAppWentToBg = true;
//            Toast.makeText(getApplicationContext(), "App is Going to Background", Toast.LENGTH_SHORT).show();
        }
    }



@Override
    public void onBackPressed() {

        isBackPressed = true;
        Log.d(TAG, "onBackPressed " + isBackPressed + "" + this.getLocalClassName());
        super.onBackPressed();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        isWindowFocused = hasFocus;
        if (isBackPressed && !hasFocus) {
            isBackPressed = false;
            isWindowFocused = true;
        }

        super.onWindowFocusChanged(hasFocus);
    }


@Override
    protected void onStart() {
        Log.d(TAG, "onStart isAppWentToBg " + isAppWentToBg);
        applicationWillEnterForeground();
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop ");
        applicationdidenterbackground();
    }

我建议创建一个新的布尔变量,如果它是第一次在简历中创建并处理它。

Boolean isForeGround = true;

@Override
public void onPause() {
   super.onPause();
   isForeGround = false;
}

@Override
public void onResume() {
    super.onPause();
    if(!isForeGround){
       isForeGround = true;
       // write your code here
    }
}

尽管它是重复的。这是我为了帮助而分享的 Java 实现 ..

public class MyApplication extends MultiDexApplication implements LifecycleObserver {
    private boolean previouslyInBackground;
    @Override
    public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    void onAppBackgrounded() {
        previouslyInBackground=true;

    }
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    void onAppForegrounded() {
        if(previouslyInBackground){
           // Do your stuff Here
        }
        previouslyInBackground=false;
    }
}

Lifecycle-aware components Documentation

添加 Gradle 依赖项