|
|
1
10
我不知道有一种方法可以获取当前显示的活动,但是可以将 Activity Lifecycle 一面旗帜。 For the flag, if you've extended the Application class, that's probably a decent place to store it. For extending the application class, the top answer to this question 有信息。(d)。 So probably keep track of the current active activity (or a flag that the activity is visible) in onResume/onPause or onStart/onStop depending on exactly what behavior you want. 因为您有多个活动,所以需要一个质心位置来存储标志,这就是为什么应用程序有意义。您可以通过强制转换应用程序上下文(例如((myApplication)getApplicationContext()).ismyActivityActive)来获取自定义应用程序对象。 您还可以扩展活动以帮助保持代码的整洁和包含性。 If you're using a service you could bind to the service in every activity in the onStart/onStop (or onResume/onPause). If bound, you're visible. |
|
|
2
4
您可以从ActivityManager请求正在运行的任务:
从API文档:
所以列表中的第一项是最前面的活动。 |
|
|
3
3
|
|
|
4
2
You could use onWindowFocusChanged(boolean hasFocus) place this in a superclass of your activities to launch the intent if it has focus. |
|
|
5
0
您可以通过代码来完成一个简单的技巧,而不是使用活动管理器。如果您仔细观察活动周期,两个活动和前台到后台的流程如下。假设A和B是两个活动。 When transition from A to B: 1. onPause() of A is called 2. onResume() of B is called 3. onStop() of A is called when B is fully resumed 当应用程序进入后台时:1.的onPause()称为2。调用的OnStop()。 只需在活动中放置一个标志,就可以检测到您的后台事件。 制作一个抽象的活动,并从其他活动中扩展它,这样您就不必在需要后台事件的任何地方复制粘贴所有其他活动的代码。 在抽象活动中,创建标记isappinbackground。 在onCreate()方法中:isAppinBackground=false; 在onPause()方法中:isAppinBackground=false; 在onstop()方法中:isappinbackground=true; 如果isAppinBackground为真,则只需检查onResume()。 n检查标志后,再次设置isappinbackground=false 对于两个活动之间的转换,因为第一个的onstop()将始终在第二个活动恢复后调用,标志永远不会为true,当应用程序处于后台时,将在onpause后立即调用活动的onstop(),因此稍后打开应用程序时,标志将为true。 |
|
Murilo · Jetpack编写导航栏项目图标 8 月前 |
|
|
KolaYAndr · 活动RESULT_OK似乎从未发生过 8 月前 |
|
psycho_pat · Android应用程序中的权限 9 月前 |
|
|
FarazFiroz · 如何将argb转换为描述性文本颜色 9 月前 |
|
Daniel · Unity Android游戏支持的设备数量太少 9 月前 |