Android Application Suspend Resume
My android application has multiple Activities. I need to perform a few things when the Application resumes (Application not Activity).
Android application provide开发者_如何学Gos onCreate callback but no onResume callback. Is there any way I can identify that my application has resumed?
Applications do not get paused or resumed, only individual activities do. You can think of the Application
class as of a static object that gets created before any of your activities (that's why it has a onCreate()
function) and gets destroyed when the system kills the process (no onDestroy()
involved here).
I believe the only way you can do that is through the Activity. If you're looking to call something across all your Activities in the onResume you can probably use a static singleton as is suggested in the Application docs:
There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.
Activities do have onResume() see here: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
@Override
protected void onResume() {
// ...
}
So when your application resumes - one of your activities will resume - handle it in each activity.
Use ProcessLifecycleOwner ProcessLifecycleOwner .
It will dispatch Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME events, as a first activity moves through these events.
And Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP, events will be dispatched with a delay after a last activity passed through them
精彩评论