【问题标题】:Check if activity onStop is called while starting a new activity检查是否在启动新活动时调用了活动 onStop
【发布时间】:2017-02-25 12:16:57
【问题描述】:

我正在尝试解决一个问题。在我的应用程序中,我需要确定 onStop 方法是因为启动新活动而被调用,还是在用户单击主页按钮或切换到另一个应用程序后被调用。

我有 BaseActivity 类,我需要在这里检查一下。

我试图找到一种方法来做到这一点,但不幸的是仍然没有找到解决方案。

也许有一个解决方法。

想法是区分onStop方法调用的发起者。

如果有任何帮助,我将不胜感激。

【问题讨论】:

  • 您可以在启动此otherActivity 时添加protected boolean otherActivityCalled = false; 并将其设置为true 并在您的onStop 挂钩中检查它。如果是false,则表示您的currentActivity 由于其他原因已停止。

标签: android performance android-activity android-lifecycle


【解决方案1】:

一种可能的解决方案是注册一个 ActivityLifecycleCallbacks 并保存最后一个调用 onResume 的活动的引用名称:

public class ActivityChecker implements Application.ActivityLifecycleCallbacks {
  private static ActivityChecker mChecker;
  private String mCurrentResumedActivity = "";

  public static ActivityChecker getInstance() {
    return mChecker = mChecker == null ? new ActivityChecker() : mChecker;
  }

  // If you press the home button or navigate to another app, the onStop callback will be called without touching the mCurrentResumedActivity property.
  // When a new activity is open, its onResume method will be called before the onStop from the current activity.
  @Override 
  public void onActivityResumed(Activity activity) {
    // I prefer to save the toString() instead of the activity to avoid complications with memory leaks. 
    mCurrentResumedActivity = activity.toString();
  }   


  public boolean isTheLastResumedActivity(@NonNull Activity activity) {
    return activity.toString().equals(mCurrentResumedActivity);
  }

  // [...] All other lifecycle callbacks were left empty
}

ActivityLifecycleCallbacks 可以在你的 Application 类中注册:

public class App extends Application {

  public App() {
    registerActivityLifecycleCallbacks(ActivityChecker.getInstance());
  }

}

不要忘记在清单中注册它:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name">    

    <application
        ...
        android:name=".App"
        > ...
    </application>
</manifest>

然后,您可以在基础 Activity 中使用它。

public class MyBaseActivity {
  @Override protected void onStop() {
    if(ActivityChecker.getInstance().isTheLastResumedActivity(this)) {
      // Home button touched or other application is being open.
    }
  }
}

参考资料:

注册您的自定义应用程序类和 ActivityLifecycleCallbacks:https://developer.android.com/reference/android/app/Application.html

写完这篇文章后,我发现这个链接有一些其他选项来检索当前恢复的活动:How to get current foreground activity context in android?

【讨论】:

    【解决方案2】:

    您可以使用 SharedPreferences 来检查它:

    @Override
        protected void onStop() {
            super.onStop();
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
            pref.edit().putBoolean("IfOnStopCalled", true).apply();
        }
    

    检查您的 BaseActivity:

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean IfOnStopCalled = pref.getBoolean("IfOnStopCalled",false);
    
    if(IfOnStopCalled){
      //Do your action
    }
    else{
     //Do your action
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-02
      • 2016-12-14
      • 2013-06-17
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      相关资源
      最近更新 更多