从后台返回活动时要做的事情

时间:2019-05-17 08:14:14

标签: android activity-lifecycle

我只想从后台返回应用程序时执行功能。

我已经在onResume中包含了该方法,并且在一定程度上做到了。问题在于,即使在创建onResume时也触发了Activity,并且从另一个活动返回到活动时(例如:从按“后退”按钮返回)也触发了该功能。

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

Ps:我的应用程序已经使用startActivity放置了多个位置,因此更改为startActivityForResult是一项繁琐的任务。

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

编辑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
    }
}

5 个答案:

答案 0 :(得分:1)

您可以使用startActivityForResult代替startActivity。 然后您可以捕获onActivityResult内部返回的方法。

答案 1 :(得分:1)

尽管它是重复的。这是我为了帮助而共享的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依赖项

答案 2 :(得分:0)

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

boolean isPaused = false; 

现在在您的活动中设置方法:-

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

或您的onResume方法中:-

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

          }
super.onResume();
    }

答案 3 :(得分:0)

这样做

将这些变量添加到您的主要活动中

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

答案 4 :(得分:-1)

我建议创建一个新的布尔变量,该变量说明是否在履历表中首次创建该变量并对其进行处理。

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
    }
}

希望这会有所帮助。