Post Delay Method - 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(); } 
+2
source share
3 answers

You can use the removeCallbacks(runnable) method of the handler with which you call the postDelayed() method. For example, if you used:

 mHandler.postDelayed(mRunnable, mTime) 

to update activity, then use

 mHandler.removeCallbacks(mRunnable) 

in the onPause() activity method.

+4
source

Make a boolean sign in your postdelayed method. Initiate the sign as true, when the activity is completed, set the sign to false.

0
source

You can use this piece of code to run after a 3 second delay.

 new Timer().schedule(new TimerTask() { @Override public void run() { // run your code here } }, 3000); 
0
source

All Articles