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....
Asked
Active
Viewed 835 times
1 Answers
-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