简体   繁体   English

如何从上下文值中知道当前正在运行哪个活动?

[英]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都将同时运行(在背景和前景中都运行)。

  1. 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? 是否可以从上下文值中检查活动当前正在运行还是已停止?

  2. 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. 因此,我不希望开发人员执行此操作,如果我可以在自己的库中执行此操作,那将是最好的。

  1. 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? 是否可以从上下文值中检查活动当前正在运行还是已停止?
  • The easiest and recommended way will be to hand a reference to the 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()回调)。
  • Another less preferable option, is that each activity will notify your library code about it's events, but it's of course more tedious and error prone. 另一个不太可取的选择是,每个活动都会将其事件通知您的库代码,但这当然更加乏味且容易出错。
  • Third 'do not do it' way is to use 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 ,从它们中您可以获取顶级活动,但是再次记录为“仅出于调试目的”,并且比以上所有内容都更容易出错。
  1. 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.

相关问题 当我们从后台进入前台时,我们如何知道当前正在加载哪个片段? - How can we know that in an activity which fragment is loaded currently when we come in foreground from background? 我如何知道当前的前台活动(不是来自服务)? - How can i know current foreground activity(not from service)? 如何在调试应用程序时检查当前正在运行的活动 - How to check the which activity is currently running while debugging the app 如何检查Android Studio上当前正在运行的活动? - How to check which activity is currently running on android studio? 如何断言在前台运行的活动? - How to assert the activity running in foreground? 从和活动更新当前正在运行的BroadcastReceiver(向其传递新值) - Update a currently running BroadcastReceiver (pass it new values) from and Activity 如何从服务检查活动是否在后台/前台运行? - How to check if an activity is running in background/foreground from a service? 如何在android中获取当前的前台活动上下文? - How to get current foreground activity context in android? 获取前台活动的上下文 - Getting context of foreground activity 如何知道当前是否有活动实例? - How to know if there is an Activity Instance currently living?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM