[英]How to know from context values which activity is currently running foreground?
I have a tricky situation. 我的处境很棘手。 I will try to fully explain it in the best possible way.
我将尽力以最佳方式充分解释它。
My app has few activities, say A, B, C. A is the Main Activity and B & C gets called from A. So basically when B or C is running, A will be stopped(onStop()). 我的应用程序几乎没有活动,例如A,B,C。A是主要活动,而B和C从A调用。因此,基本上,当B或C运行时,A将停止(onStop())。
I am using an android library(created by me) in my App. 我在我的应用程序中使用了一个android库(由我创建)。 My library saves the contexts of the activities.
我的图书馆保存了活动的上下文。 For example, when we open the app with activity A, my library will save A's context and will create a handler using that context and save them in a Map(
Map<Context, Handler
>). 例如,当我们使用活动A打开应用程序时,我的库将保存A的上下文,并将使用该上下文创建处理程序,并将其保存在Map(
Map<Context, Handler
>)中。 Now if we navigate to activity B, my library will get B's context and then make a new entry for this in the Map. 现在,如果我们导航到活动B,我的图书馆将获得B的上下文,然后在地图中为此添加一个新条目。
At some point in time, I will use the handlers present in the Map to post some runnables. 在某个时间点,我将使用Map中存在的处理程序来发布一些可运行对象。 Obviously, both A and B will receive as both are running(one in background and foreground).
显然,A和B都将同时运行(在背景和前景中都运行)。
But how can I check and send only to the one running in the foreground? 但是,如何检查并仅发送给在前台运行的服务器呢? Is it possible to check from a context value, whether the activity is currently running or stopped?
是否可以从上下文值中检查活动当前正在运行还是已停止?
Does this.getMainLooper
when called from A and B return the same thing? 从A和B调用
this.getMainLooper
是否会返回相同的东西?
UPDATE: I am sorry, I missed this point. 更新:对不起,我错过了这一点。 The App is a demo application on how to use my library.
该应用程序是有关如何使用我的库的演示应用程序。 So I will release my library in future and developers will be using it.
因此,我将来会发布我的库,而开发人员将使用它。 Hence I don't want the developers to do this and it would be the best if I can do it in my library itself.
因此,我不希望开发人员执行此操作,如果我可以在自己的库中执行此操作,那将是最好的。
- But how can I check and send only to the one running in the foreground?
但是,如何检查并仅发送给在前台运行的服务器呢? Is it possible to check from a context value, whether the activity is currently running or stopped?
是否可以从上下文值中检查活动当前正在运行还是已停止?
Application
object to your library, with it you can register to Activity Lifecycle callbacks using Application.registerActivityLifecycleCallbacks , and monitor the state of the Activities and determine when activity is foreground (using onActivityResumed()
callback). Application
对象的引用交给您的库,使用它,您可以使用Application.registerActivityLifecycleCallbacks注册到Activity Lifecycle回调,并监视Activity的状态并确定何时活动为前台(使用onActivityResumed()
回调)。 ActivityManager
that has getRunningTasks()
, and from their you can get top activity, but again it's documented as for 'debug purposes only' and is more error prone then all the above. getRunningTasks()
ActivityManager
,从它们中您可以获取顶级活动,但是再次记录为“仅出于调试目的”,并且比以上所有内容都更容易出错。
- Does this.getMainLooper when called from A and B return the same thing?
从A和B调用this.getMainLooper是否会返回相同的东西?
Yep, A and B sitting on the same process, Main Looper is the looper of the main UI thread, which is singular per Process
. 是的,A和B坐在相同的过程,主要是活套在主UI线程,这是每单数的弯针
Process
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.