how to check if activity is still in the stack?

前端 未结 3 510
天涯浪人
天涯浪人 2021-01-12 14:05

what is the better way to check if the activity is still in the stack in order to call it back ?

Intent i = new Intent(getApplicationContext(),MyClass.class)         


        
3条回答
  •  执念已碎
    2021-01-12 14:25

    You can toggle global variable as indicator inside onCreate() and onDestory() of specific class, OR inside onActivityCreated() and onActivityDestroyed() of ActivityLifecycleCallbacks.

    e.g.:

      registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
    
          @Override
          public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
              if (activity instanceof YourActivity) {
                  myGlobalData.setActExist(true);
              } 
          }
    
          @Override
          public void onActivityDestroyed(Activity activity) {
              if (activity instanceof YourActivity) {
                  myGlobalData.setActExist(false);
              }
          }
      });
    

提交回复
热议问题