I am posting this question as I did not find the perfect solution. I want to logout user if he/she has pressed home button or is inactive on app for 5 minutes.
I have tried to use a timer as below(kotlin code),but thats not really the right way,that will just work for a single activity screen,not entire app:
timer = object : CountDownTimer((1 * 60 * 1000).toLong(), 1000) {
override fun onTick(millisUntilFinished: Long) {
Toast.makeText(this@MainActivity, "Ticking", Toast.LENGTH_LONG).show()
}
override fun onFinish() {
currentUser = mAuth!!.currentUser!!
// Code for Logout
Toast.makeText(this@MainActivity, "Finished", Toast.LENGTH_LONG).show()
val colref = mFirestore.collection("AllUsers")
deleteCollectiontimer(colref, EXECUTOR)
}
}
override fun onResume() {
super.onResume()
Toast.makeText(this@MainActivity, "Timer Stopped", Toast.LENGTH_LONG).show()
timer.cancel()
}
override fun onStop() {
super.onStop()
Toast.makeText(this@MainActivity, "Timer Started", Toast.LENGTH_LONG).show()
timer.start()
}
But if I put the above code in every activitys onstop() and onresume(),that is not right.So I am confused on how to logout user when inactive on entire app?
I also read something like the ProcessLifecycleowner,but did not quite understand how to use it,so can that help me in this case,if yes then how?
Is there a particular lifecycle method or a direct code which will take care for inactivity of entire application?
Any help is appreciated.