如何检查我的活动是否是在屏幕中运行的当前活动 [英] How to check if my activity is the current activity running in the screen

查看:21
本文介绍了如何检查我的活动是否是在屏幕中运行的当前活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Toast 来做通知,但即使它的活动不在当前屏幕中并且其他活动已经启动,它似乎也会出现.

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 ?

推荐答案

当你的 Activity 出现在前台时,它的 onResume() 方法将被调用.当另一个 Activity 出现在您的 Activity 前面时,它的 onPause() 方法将被调用.所以你需要做的就是实现一个布尔值,指示你的 Activity 是否在前台:

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

这篇关于如何检查我的活动是否是在屏幕中运行的当前活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆