0

I have created a handler in my app. The functionality of this handler is that if it doesn't detect any user input for 15 seconds(touch,drag etc) it will launch a new activity. Here is the code for the same.

public void startHandler(long duration) {
    if (handler != null) {
        handler.postDelayed(r, duration);
        Log.d("handler", "inside if");
    } else {
        Log.d("handler", "inside else");
        handler = new Handler();
        r = new Runnable() {

            @Override
            public void run() {

                Log.d("handler", "handler running");
                // TODO Auto-generated method stub
                Intent intent = new Intent(ActivityProducts.this, ActivityVideo.class);
                startActivity(intent);
            }
        };
        handler.postDelayed(r, duration);
    }
}

Here the duration is dynamic and is fetched from the server.

Below is the code to stop handler.

public void stopHandler() {
    if (handler != null) {
        handler.removeCallbacks(r);
        handler.removeCallbacksAndMessages(null);
        handler = null;
    }
}

And below is the code for starting handler

@Override
public void onUserInteraction() {
    // TODO Auto-generated method stub
    super.onUserInteraction();
    Log.d("user", "interacted");
    //stop first and then start
    if (duration != 0) {
        stopHandler();
        startHandler(duration);
    }
}

I am stopping the handler in all methods onPause,onStop and onDestroy. But the problem is that if I navigate to any other activity from this activity the start handler code still executes and takes me to the Video Activity.

So what is the proper way of stopping the handler?

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • Perhaps you are winding up with multiple `Handler` instances somehow. Since `postDelayed()` and `removeCallbacks()` are both on `View`, perhaps you could try using some existing `View` in your layout, rather than creating a `Handler`. – CommonsWare Apr 26 '18 at 06:15
  • Can you include the code where you are calling `stopHandler()` when leaving the activity? – waqaslam Apr 26 '18 at 06:15
  • @waqaslam I have mentioned that. I am calling stop handler method in onPause,onStop,onDestroy – Vivek Mishra Apr 26 '18 at 06:19
  • @CommonsWare sir can you give an example of what you are trying to say ? How to use existing views ? – Vivek Mishra Apr 26 '18 at 06:20
  • 4
    [This sample app](https://github.com/commonsguy/cw-omnibus/tree/v8.11/Threads/PostDelayed) demonstrates using `postDelayed()` and `removeCallbacks()` without a `Handler`. – CommonsWare Apr 26 '18 at 06:22
  • @CommonsWare so can I use any view from my activity's layout or it has to be the parent layout ? – Vivek Mishra Apr 26 '18 at 06:24
  • I do not know what "the parent layout" means. AFAIK, any `View` should work for `postDelayed()` and `removeCallbacks()`. – CommonsWare Apr 26 '18 at 06:29
  • Okay. By parent layout I meant to say the root element of the layout file like relative or linear layout – Vivek Mishra Apr 26 '18 at 06:34
  • VivekMishra It is the root layout which @Commonsware has mentioned. i.e. 'root=findViewById(android.R.id.content);' – maveroid Apr 26 '18 at 06:36
  • In that sample, I happen to use the `android.R.id.content` `View`, but that is not required. – CommonsWare Apr 26 '18 at 06:38
  • I tried with the approach mentioned in the link but the toast fired immediately. How can I switch to the activity after the time defined in post Delayed ? – Vivek Mishra Apr 26 '18 at 06:38
  • possible of dublicate https://stackoverflow.com/questions/22718951/stop-handler-postdelay – Amjad Khan Apr 26 '18 at 06:39
  • @AmjadKhan Before marking duplicate please read the content of the question too. I am already doing everything mentioned in that link – Vivek Mishra Apr 26 '18 at 06:41

0 Answers0