I have to maintain statistics based on session in my application. Problem is that android never closes the application even when the user exits the application using the back button (might be because I’m running services in the background).
I would like to reset my session information if the application is in the background for say X minutes (to differentiate it from say a phone call or alarm). Is there any way to do that?
Maybe a some sort of a broadcast receiver to detect the event … ?
Let me try to explain the scenario a little bit. There is this part of my application which can be accessed only after authentication (entering a password). Now once the user is inside this part, I wouldn’t want to keep asking the user for authentication every time the activity is paused. But say the user presses the Home key and returns to the application after say, 20 minutes, I would like to ask for authentication.
EDIT : I am working with multiple activities, so I cannot detect the application going to the background simply setting a flag in the onPause().
Call in onStop. onStop gets called after onStart of whatever takes over the screen – if its an activity in the same apk package then you’re not going into the background. This does require the GET_TASKS permission, and if an activity comes up without you backing out it’ll still trigger (user selects something from the notification bar or something).
Or bind to a service onStart & unbind onStop – the service will then be onDestroyed when all your activities are stopped (or track binds vs unbinds if you don’t want to rely on onDestroyed getting called – because it might not..).