2

I am implementing an inactivity timer based on this answer: https://stackoverflow.com/a/12481918/6298161

I need the timer to work across multiple activities, so as the comments suggest from the original post I have change the Handler and Runnable to be static.

How do I now redirect to a new activity in the runnable where I have put the comment? Any help is greatly appreciated

public class InactivityTimerActivity extends AppCompatActivity {


    public static final long DISCONNECT_TIMEOUT = 300000; // 5 min = 5 * 60 * 1000 ms

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inactivity_timer);
    }


    private static Handler disconnectHandler = new Handler() {
        public void handleMessage(Message msg) {
        }
    };

    private static Runnable disconnectCallback = new Runnable() {
        @Override
        public void run() {
            // I want to redirect here
        }
    };

    public void resetDisconnectTimer() {
        disconnectHandler.removeCallbacks(disconnectCallback);
        disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
    }

    public void stopDisconnectTimer() {
        disconnectHandler.removeCallbacks(disconnectCallback);
    }

    @Override
    public void onUserInteraction() {
        resetDisconnectTimer();
    }

    @Override
    public void onResume() {
        super.onResume();
        resetDisconnectTimer();
    }

    @Override
    public void onStop() {
        super.onStop();
        stopDisconnectTimer();
    }
}
Community
  • 1
  • 1
teiiluj
  • 241
  • 3
  • 10

1 Answers1

0

I think you shouldn't make it static. Just keep that protected, and then when start new activity, putting an integer variable inside the intent (the remaining time or the time that the timer has run). Then when on create new activity, you get that value out and set your timer base on that value.

Nguyen Quang Anh
  • 315
  • 1
  • 3
  • 12