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!
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!
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;