Search code examples
javaandroidandroid-activity

Logout after a specific time of inactivity


I want to logout users after they have been inactive for 3 minutes on my app i.e doing nothing for 3 minutes. I have only one Activity and it contains multiple Fragments. How can I achieve this? I tried the answers posted on the same topic but nothing worked. Please can anyone be specific?


Solution

  • Try this

    Use CountDownTimer

    CountDownTimer timer = new CountDownTimer(15 *60 * 1000, 1000) {
    
            public void onTick(long millisUntilFinished) {
               //Some code
            }
    
            public void onFinish() {
               //Logout
            }
         };
    

    When user has stopped any action use timer.start() and when user does the action do timer.cancel()