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?
            Asked
            
        
        
            Active
            
        
            Viewed 648 times
        
    2
            
            
        
        Doron Yakovlev Golani
        
- 5,188
 - 9
 - 36
 - 60
 
        Shahzaib Ali
        
- 41
 - 6
 
4 Answers
2
            
            
        Each Activity has an onUserInteraction() function. You can override it and reset a Runnable. If there is no user interaction the Runnable will be called after 3 minutes. Don't forget to remove Runnable onDestory().
Runnable r = new Runnable() {
    @Override
    public void run() {
        // handle timeout
    }
};
Handler handler = new Handler();
@Override
public void onUserInteraction() {
    super.onUserInteraction();
    handler.removeCallbacks(r);
    handler.postDelayed(r, 3 * 60 * 1000 );
}
@Override
protected void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(r);
}
        Doron Yakovlev Golani
        
- 5,188
 - 9
 - 36
 - 60
 
- 
                    You cannot use this as the user might close the activity and the handler will leak the memory. – Adib Faramarzi Mar 26 '18 at 18:43
 - 
                    @AdibFaramarzi that is why you remove the callback on destroy. – Doron Yakovlev Golani Mar 26 '18 at 18:46
 - 
                    That wouldn't work. Either you remove the callback onDestory and you lose the callback (and the user will never be logged out when they close the app) or you don't remove it and get memory leak. – Adib Faramarzi Mar 26 '18 at 18:47
 - 
                    What is thisNavigatorImpl.instance()? – Shahzaib Ali Mar 27 '18 at 11:10
 
0
            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()
        Kristofer
        
- 809
 - 9
 - 24
 
0
            
            
        You have two options.
- Use an alarm manager and have a broadcast receiver upon receiving the alarm and log out from there. When the user does some activity, restart the alarm. example.
 - have a Service that you can talk to, and send intents 
restartTimer. When the timer reaches zero, log out the user. 
        Adib Faramarzi
        
- 3,798
 - 3
 - 29
 - 44