How to kill PostDelayed method in Android

I used the Published Method method to update my activity, which is working fine. But the problem is that even when I click the back button, the postdelayed method calls the previous activity.

// handler for 30,000 milliseconds after an activity update delay

mHandler.postDelayed(new Runnable() { public void run() { dostuff(); } }, 30000); } protected void dostuff() { Intent intent = getIntent(); finish();startActivity(intent); Toast.makeText(getApplicationContext(), "refreshed", Toast.LENGTH_LONG).show(); } public void onBackPressed() { super.onBackPressed(); finish(); mHandler.removeCallbacks(null); } protected void onStop() { mHandler.removeCallbacks(null); super.onStop(); } 
+6
source share
3 answers

you just use it, it can help you

  Runnable runobj=new Runnable() { public void run() { dostuff(); } }; mHandler.postDelayed(runobj, 30000); } public void onBackPressed() { super.onBackPressed(); mHandler.removeCallbacks(runobj); } 
+6
source

Workaround for this:

When executing the dostuff () method, just check if Activity.isFinising () and do are really. If it ends, just come back.

When you return, the action will complete, and after that, if doStuff () executes, it will do nothing.

+2
source

mHandler.removeCallbacks (zero) ;. You are passing null as a parameter. Pass Runnable onject. That should work.

 public final void removeCallbacks (Runnable r) 

Use the above.

Added to API Level 1 Remove any pending Runnable r messages that are in the message queue.

http://developer.android.com/reference/android/os/Handler.html#removeCallbacks%28java.lang.Runnable%29

Edit: Example

 Runnable runnable = new Runnable() { @Override public void run() { // execute some code } }; Handler handler = new Handler(); handler.postDelayed(runnable, 10000); handler.removeCallbacks(runnable); 
+1
source

All Articles