I want to store user logged out time in the database(for maintaining the user analytics) when the android app forcibly closed by user. Is it possible to capture event in MainActivity?
can any one suggest the better approach to achieve this?
I want to store user logged out time in the database(for maintaining the user analytics) when the android app forcibly closed by user. Is it possible to capture event in MainActivity?
can any one suggest the better approach to achieve this?
there is no reliable way for this event detection - this would be kind-of insecure to inform app that it's going to be killed allowing to execute some code at that moment (as in your case - database handling). user clearly wants to stop this app, when it would get a possibility to execute some code just before gets killed then it may e.g. leave some alarm for now + 10secs in AlarmManager
to restart some background service. this is opposite to what user wanted
best you can do is listening when last Activity
is destroyed (onDestroy
call), but this may occur in multiple cases, no ony force stop app by user. use some static int
and ++
when Activity
is created and --
when destroyed or use Application.ActivityLifecycleCallbacks
on never APIs