How to know which activity is open, running?

0

I have two activities, Activity1 and Activity2.

I need to know if 2 is active or not in both activities.

Example:

if (activityrunning == activity2){
   //do something
}else{
  //do something else
}

Thank you!

    
asked by anonymous 01.02.2017 / 20:41

1 answer

3

One way to do this is to create a static variable and change it according to the activity lifecycle. For example, when the onStart() method executes the variable it receives true . If you run onStop() , then the variable will get the value false . See below:

class MinhaActivity extends Activity {
     static boolean status = false;

      @Override
      public void onStop() {
         super.onStop();
         status = false;
      }

      @Override
      public void onStart() {
         super.onStart();
         status = true;
      } 
}

Because the variable is static, you can access your variable from any activity . See:

OutraActivity.status;
    
01.02.2017 / 20:56