By background, I mean none of the application's activities are currently visible to the user?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Starting support library version 26 you can use ProcessLifecycleOwner, just add it to your dependency like described here, for example:
And then just query
ProcessLifecycleOwner
whenever you want for app state, examples:To piggyback on what CommonsWare and Key have said, you could perhaps extend the Application class and have all of your activities call that on their onPause/onResume methods. This would allow you to know which Activity(ies) are visible, but this could probably be handled better.
Can you elaborate on what you have in mind exactly? When you say running in the background do you mean simply having your application still in memory even though it is not currently on screen? Have you looked into using Services as a more persistent way to manage your app when it is not in focus?
See the comment in the onActivityDestroyed function.
Works with SDK target version 14> :
I recommend reading through this page: http://developer.android.com/reference/android/app/Activity.html
In short, your activity is no longer visible after
onStop()
has been called.You can use ComponentCallbacks2 to detect if the app is in background. BTW this callback is only available in API Level 14 (Ice Cream Sandwich) and above.
You will get a call to the method:
public abstract void onTrimMemory (int level)
if the level is
ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
then the app is in background.You can implement this interface to an
activity
,service
, etc.