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

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

问题描述

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



我知道有关如何查看您的活动是否在前台的许多问题,但我不知道这是否允许在活动之上的对话框。



这是问题,红色是我的活动,在对话的前台是背景中的





编辑:我已经尝试只使用 finish(),但是我的活动可以退回在我试图避免的堆栈应用程序中。

解决方案

这是推荐的 / strong>解决方案:


正确的解决方案(信用转到Dan,CommonsWare和NeTeInStEiN)
通过以下方式跟踪您的应用程序的可见性你自己使用
Activity.onPause,Activity.onResume方法。在其他类中存储可见性状态
。您可以自行执行
应用程序或服务(如果您想要检查服务的活动可见性,则还可以使用此
解决方案的一些变体)。



示例
实现自定义应用程序类(注意isActivityVisible()静态方法):




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




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




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




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



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

@Override
protected void 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天全站免登陆