如何检查活动是在前台还是在可见背景中?

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

问题描述

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

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

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

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

解决方案

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

<块引用>

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

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

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

<块引用>

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

<块引用>

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

@Override受保护的无效 onResume() {超级.onResume();MyApplication.activityResumed();}@覆盖受保护的无效 onPause() {超级.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

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

上一篇:如何更改 Android 5.0 的 DatePicker 对话框颜色 下一篇:启用 GPS 的 LocationSettingsRequest 对话框 - 跳过了 onActivityResult(

相关文章