I have a code as below:

```
myelement.postDelayed(new Runnable() { 
    public void run() {
        myelement.setVisibility(View.GONE);
    } }, 8000);

```

I want this to remove callback everytime onStop() or onPause() happens.

I read in other SO articles you could do:

```
handler.removeCallbacks(null);
handler.removeCallbacksAndMessages(null);
handler.removeCallbacks(runnable);

```

When I tried to do:

```
public synchronized void onPause() {        // on pause
    myelement.removeCallbacks(null);
    super.onPause();                        
}

```

and tried to go back and forth between activities. When I tried to do this, however, the element just glitched like sometimes showing the element for half a second, sometimes a full second, sometimes it works. etc.