Issue
I know for an activity you can override onStart on onStop methods to know when an activity starts / exits. The issue I'm running into is I want to keep a session open from when a user opens the app until it enters the background / user exits and tying into onStop (for end session) for each activity isn't giving the the results I want, it ends the session every new activity. So I was wondering what are my options for knowing when a user puts the app in the background or exits.
One thing I thought of is keeping track of onStart and onStop (or any two combinations in the activity life cycle) so I know if I have a onStop without a onStart right before the app is exiting. This seems very hacky, not sure if its the right place to start.
Thanks for any input.
Solution
You can extend Application
class and use registerActivityLifecycleCallbacks
to build the logic.
Using for example these two callbacks:
@Override
public void onActivityResumed(Activity activity) {
mBackground = false;
}
@Override
public void onActivityPaused(Activity activity) {
mBackground = true;
}
Answered By - Niko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.