如何检查,如果我的行为是在屏幕上运行的当前活动我的、是在、行为、屏幕上

2023-09-12 04:34:46 作者:旧时光

我用面包做出通知,但现在看来,这就会出现,甚至它的活动是不是在当前屏幕和其他一些活动已经开始了。

I used Toast to make notification, but it seems it will appear even its activity is not in the current screen and some other activity has been started.

我要检查这种情况下,当活动不是当前的,我不发送Toast通知。但是,该怎么办?

I want to check this situation, when the activity is not the current one, I'd not send the Toast notification. But how to do ?

推荐答案

当您的活动来到前台,其 onResume()方法将被调用。当另一个活动涉及您的活动前,其的onPause()方法将被调用。因此,所有你需要做的是实现一个布尔值,表示如果你的活动是在前台:

When your Activity comes to the foreground, its onResume() method will be invoked. When another Activity comes in front of your Activity, its onPause() method will be invoked. So all you need to do is implement a boolean indicating if your Activity is in the foreground:

private boolean isInFront;

@Override
public void onResume() {
    super.onResume();
    isInFront = true;
}

@Override
public void onPause() {
    super.onPause();
    isInFront = false;
}