-3

I am new to android programming.In my app I have two activities A and B.Activity A is a Home page from which Activity B is also launched.I am having problem regarding launching the activity is that before Launching Activity B i want to check weather Activity B is already launched/running or not.So how can I do that....

hamid_c
  • 849
  • 3
  • 11
  • 29
Adarsh
  • 5
  • 2
  • explain your problem more specifically with your code and other stuff so that people can understand your problem and try to help you out. – V-rund Puro-hit May 07 '16 at 07:00

1 Answers1

-1

You can check it through a static variable or can save a variable in SharedPreference to check whether activity is active or not.

Like in case of static varibale:

class MyActivity extends Activity {
     static boolean active = false;

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

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

or in case of SharedPreference:

 public class example extends Activity {

    @Override
    protected void onStart() {
        super.onStart();

        // Store our shared preference
        SharedPreferences sp = getSharedPreferences("Active", MODE_PRIVATE);
        Editor ed = sp.edit();
        ed.putBoolean("active", true);
        ed.commit();
    }

    @Override
    protected void onStop() {
        super.onStop();

        // Store our shared preference
        SharedPreferences sp = getSharedPreferences("Active", MODE_PRIVATE);
        Editor ed = sp.edit();
        ed.putBoolean("active", false);
        ed.commit();

    }
}
Android Geek
  • 8,956
  • 2
  • 21
  • 35