I am trying to write an app that does something specific when it is brought back to the foreground after some amount of time. Is there a way to detect when an app is sent to the background or brought to the foreground?
相关问题
- 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
How about this solution
All Activity need to extends BaseActivity.
When an activity call another (A->B) then currentAct is not equal getLocalClassName() because the onStart() of the second activity (B) is called before the onStop() of the first (A) (https://developer.android.com/guide/components/activities.html#CoordinatingActivities).
When the user press the home button or change between application will just call onStop() and then currentAct is equal getLocalClassName().
Consider using onUserLeaveHint. This will only be called when your app goes into the background. onPause will have corner cases to handle, since it can be called for other reasons; for example if the user opens another activity in your app such as your settings page, your main activity's onPause method will be called even though they are still in your app; tracking what is going in will lead to bugs when you can instead simply use the onUserLeaveHint callback which does what you are asking.
When on UserLeaveHint is called, you can set a boolean inBackground flag to true. When onResume is called, only assume you came back into the foreground if the inBackground flag is set. This is because onResume will also be called on your main activity if the user was just in your settings menu and never left the app.
Remember that if the user hits the home button while in your settings screen, onUserLeaveHint will be called in your settings activity, and when they return onResume will be called in your settings activity. If you only have this detection code in your main activity you will miss this use case. To have this code in all your activities without duplicating code, have an abstract activity class which extends Activity, and put your common code in it. Then each activity you have can extend this abstract activity.
For example:
I found a good method to detect application whether enter foreground or background. Here is my code. Hope this help you.
}
This is the modified version of @d60402's answer: https://stackoverflow.com/a/15573121/4747587
Do everything mentioned there. But instead of having a
Base Activity
and making that as a parent for every activity and the overriding theonResume()
andonPause
, do the below:In your application class, add the line:
registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback);
This
callback
has all the activity lifecycle methods and you can now overrideonActivityResumed()
andonActivityPaused()
.Take a look at this Gist: https://gist.github.com/thsaravana/1fa576b6af9fc8fff20acfb2ac79fa1b
Create a class that extends
Application
. Then in it we can use its override method,onTrimMemory()
.To detect if the application went to the background, we will use:
My solution was inspired by @d60402's answer and also relies on a time-window, but not using the
Timer
:where the
SingletonApplication
is an extension ofApplication
class: