2

I used postedDelayed method to refresh my Activity, which works fine. But the problem is that even when I press the Back button postdelayed method call back the previous activity..

//handler for 30000 milli-secs post delay refreshment of the activity

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();
 }
asked Apr 17, 2013 at 3:13
1
  • Please post your code ........to check the problem Commented Apr 17, 2013 at 4:25

3 Answers 3

4

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

mHandler.postDelayed(mRunnable, mTime)

for refreshing the activity, then use

mHandler.removeCallbacks(mRunnable)

in onPause() method of the activity.

answered Apr 17, 2013 at 3:52
Sign up to request clarification or add additional context in comments.

1 Comment

It is still making the Application crash and i am not getting any logs for this.
0

Make a sign of boolean in your postdelayed method. Init the sign as true,when the activity is finnished , set the value of sign as false.

answered Apr 17, 2013 at 3:36

1 Comment

Your answer's not clear. Perhaps a code sample would make this easier to understand.
0

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

new Timer().schedule(new TimerTask() { 
 @Override
 public void run() {
 // run your code here 
 }
}, 3000);
answered Jun 6, 2014 at 10:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.