如何检查活动是在前景中还是在可见背景中? [英] How to check if activity is in foreground or in visible background?

查看:18
本文介绍了如何检查活动是在前景中还是在可见背景中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计时器上有一个闪屏.我的问题是,在我 finish() 我的活动之前,我需要检查下一个活动是否已经开始,因为系统对话框弹出,我只想 finish();一旦用户从对话框中选择了一个选项?

我知道有很多关于如何查看您的活动是否在前台的问题,但我不知道这是否也允许在活动顶部显示对话框.

这是问题所在,红色是我的活动在后台,而对话在前台:

我曾尝试不使用 finish() 但随后我的活动可以返回到我试图避免的应用程序堆栈中.

解决方案

这是推荐的正确解决方案:

<块引用>

正确的解决方案(感谢 Dan、CommonsWare 和 NeTeInStEiN)使用自己跟踪应用程序的可见性Activity.onPause、Activity.onResume 方法.存储可见性"状态在其他一些班级.好的选择是您自己实现应用程序或服务(也有一些变体如果您想检查服务中的活动可见性,则解决方案).

示例实现自定义 Application 类(注意 isActivityVisible() 静态方法):

public class MyApplication extends Application {公共静态布尔 isActivityVisible() {返回活动可见;}公共静态无效活动恢复(){活动可见 = 真;}公共静态无效活动暂停(){活动可见 = 假;}私有静态布尔活动可见;}

<块引用>

在 AndroidManifest.xml 中注册您的应用程序类:

<应用程序android:name="your.app.package.MyApplication"android:icon="@drawable/icon"android:label="@string/app_name" >

<块引用>

为项目中的每个Activity添加onPause和onResume(你可以如果您愿意,可以为您的活动创建一个共同的祖先,但是如果您的活动已经从 MapActivity/ListActivity 等扩展而来.您仍然需要手动编写以下内容):

@Override受保护的无效 onResume() {super.onResume();MyApplication.activityResumed();}@覆盖受保护的无效 onPause() {super.onPause();MyApplication.activityPaused();}

在您的 finish() 方法中,您想使用 isActivityVisible() 来检查活动是否可见.在那里您还可以检查用户是否选择了一个选项.当两个条件都满足时继续.

消息来源还提到了两个错误的解决方案......所以请避免这样做.

来源:stackoverflow

I have a splash screen on a timer. My problem is that before I finish() my activity I need to check that the next activity has started because a system dialogue box pops-up and I only want to finish(); once the user has selected an option from the dialogue box?

I know that there are many questions on how to see if your activity is in the foreground but I do not know if this allows for dialogue boxes on top of the activity too.

Here is the problem, the red is my activity which is in the background while the dialogue is in the foreground:

EDIT: I have tried just not using finish() but then my activity can be gone back to in the stack of applications which I am trying to avoid.

解决方案

This is what is recommended as the right solution:

The right solution (credits go to Dan, CommonsWare and NeTeInStEiN) Track visibility of your application by yourself using Activity.onPause, Activity.onResume methods. Store "visibility" status in some other class. Good choices are your own implementation of the Application or a Service (there are also a few variations of this solution if you'd like to check activity visibility from the service).

Example Implement custom Application class (note the isActivityVisible() static method):

public class MyApplication extends Application {

  public static boolean isActivityVisible() {
    return activityVisible;
  }  

  public static void activityResumed() {
    activityVisible = true;
  }

  public static void activityPaused() {
    activityVisible = false;
  }

  private static boolean activityVisible;
}

Register your application class in AndroidManifest.xml:

<application
    android:name="your.app.package.MyApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

Add onPause and onResume to every Activity in the project (you may create a common ancestor for your Activities if you'd like to, but if your activity is already extended from MapActivity/ListActivity etc. you still need to write the following by hand):

@Override
protected void onResume() {
  super.onResume();
  MyApplication.activityResumed();
}

@Override
protected void onPause() {
  super.onPause();
  MyApplication.activityPaused();
}

In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.

The source also mentions two wrong solutions...so avoid doing that.

Source: stackoverflow

这篇关于如何检查活动是在前景中还是在可见背景中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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