有没有确定某一活动是否活跃什么简单的办法? 我想做的事情,这取决于活动是积极的某些事情。 例如:
如果(activityrunning =活性1)
//做这个
否则,如果(activityrunning =活性2)
//做别的事情
解决方案
您可以在活动中使用静态
变量。
类MyActivity延伸活动{
静态布尔有效=虚假的;
@覆盖
公共无效的OnStart(){
super.onStart();
积极=真实;
}
@覆盖
公共无效的onStop(){
super.onStop();
积极= FALSE;
}
}
唯一的问题在于,假如你在两项活动链接到对方,然后的onStop
上的第一个后 ONSTART 第二。所以,这两个可能是真实的简单。
根据你正在努力做的事情(更新从服务的当前活动?)。你可以只注册一个静态监听器在服务中的活动 ONSTART
方法,那么正确的监听器时,将提供你的服务要更新UI。
Is there any simple way of determining whether or not a certain activity is active? I want to do certain things depending on which activity is active. eg:
if(activityrunning= activity1)
//do this
else if (activityrunning= activity2)
//do something else
解决方案
You can use a static
variable within the activity.
class MyActivity extends Activity {
static boolean active = false;
@Override
public void onStart() {
super.onStart();
active = true;
}
@Override
public void onStop() {
super.onStop();
active = false;
}
}
The only gotcha is that if you use it in two activities that link to each other then onStop
on the first is sometimes called after onStart
in second. So both might be true briefly.
Depending on what you are trying to do (update the current activity from a service?). You could just register a static listener in the service in your activity onStart
method then the correct listener will be available when your service wants to update the UI.