Know if Activity is open (BroadcastReceiver or service) [duplicate]

0

I would like to implement a function that can check if Activity is open, because when I receive the notification through GCM (Google Cloud Messaging) if the activity has opened it will only loads the information in it, if it has closed notify the user and through this notification it opens the Activity with the information. I want to do this within a service, vlw!

    
asked by anonymous 28.09.2016 / 22:13

1 answer

1

You can use a static variable within Activity , so you can set it by exploring the life cycle onStart() and onStop() . See:

class MinhaActivity extends Activity {
     static boolean active = false;

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

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

To access and verify that the variable is active or inactive, you can do this:

if(MinhaActivity.active){
    //esta ativa
} else {
    //não está ativa
}
    
28.09.2016 / 23:04