如何检查,如果活动是在前台或后台可见? [英] How to check if activity is in foreground or in visible background?

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

问题描述

我有一个计时器启动画面。我的问题是,在我完成()我的活动我需要检查下一个活动已经开始,因为一个系统对话框弹出,而我只想完成();一旦用户选择在对话框中的选项?

我知道有就怎么看,如果你的活动是在前台的许多问题,但我不知道这是否可以进行对话盒上的活动之上了。

这是问题所在,红色是我的行为是在后台进行的对话是在前台:

编辑:我也尝试过不使用完成()但后来我的活动可以回去到应用程序的堆栈,我我试图避免的。

解决方案

这是被推荐为在右键解决方案:

  

正确的解决方案(学分去丹,CommonsWare和NeTeInStEiN)   你的应用程序自己使用轨道知名度   Activity.onPause,Activity.onResume方法。商店知名度状态   在其它的类。比较好的选择是自己实现的   应用程序或服务(也有这样一些变化   解决办法,如果你想查询从服务活动的可见性)。

     

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

 公共类MyApplication的扩展应用{

  公共静态布尔isActivityVisible(){
    返回activityVisible;
  }

  公共静态无效activityResumed(){
    activityVisible = TRUE;
  }

  公共静态无效activityPaused(){
    activityVisible = FALSE;
  }

  私有静态布尔activityVisible;
}
 

  

填写您的应用程序类在AndroidManifest.xml中:

 <应用
    机器人:名称=your.app.package.MyApplication
    机器人:图标=@可绘制/图标
    机器人:标签=@字符串/ APP_NAME>
 

  

添加的onPause和onResume每一个活动项目(可能   创建一个共同祖先的活动,如果你想,但如果   你的活动已经从MapActivity / ListActivity等扩展   你仍然需要编写以下手):

  @覆盖
保护无效onResume(){
  super.onResume();
  MyApplication.activityResumed();
}

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

在你的完成()方法,你想用 isActivityVisible()来检查活动是可见的或不。在那里,您还可以检查用户是否选择一个选项或没有。继续在满足这两个条件。

该人士还提到了两种错误的做法解决方案......所以要避免这样做。

来源:计算器

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天全站免登陆