Setup: I have a 4 stage activity that I use a postdelayed handler to pause with two more nested postdelays.
Timeline:
stage1 |CODE FIRES| stage2 |CODE FIRES| stage3 |CODE FIRES| stage4
stage1: deactivated, just shows a custom alertdialog
stage2: deactivated(mostly) with visuals for user, including countdowntimer
stage3: deactivated with 2nd countdowntimer
stage4: user interaction, no countdowns or additional pauses
I want the user to be able to skip to the end of stage2 at the press of a button, which would cancel both the countdown timer and the postdelayed (without cancelling the code it's waiting to initiate)
I've tried to cancel both and each individually but haven't succeeded, here are the commands I've used.
countdowntimer.cancel(); // this didn't work from the button or a void call
handler.removeCallbacks(runnable); // want to skip to end of stage2
I saw some references to try/catch, or maybe using a thread in searches but not sure how to use or if it's appropriate.
Thanks for any input.
**Edit for Non-Working Code reference:* I cut most of the unrelated stuff, not sure if I killed any } or ; extra
public class Main extends Activity {
public Boolean onOffCDT = true;
public Handler handler2Mem;
public Runnable mem2Runnable = null;
public CountDownTimer aCounter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
aCounter = null;
final Handler handlerMem = new Handler();
final Runnable memRunnable = new Runnable() {
public void run() {
//code
final CountDownTimer aCounter = new CountDownTimer(memTime, 100) {
public void onTick(long millisUntilFinished) {
//update time
if(onOffCDT.equals(false)) {
onOffCDT = true;
// aCounter.cancel();
}
}
public void onFinish() {
title.setText(tempTitle);
}
};
aCounter.start();
Handler handler2Mem = new Handler();
Runnable mem2Runnable = new Runnable() {
public void run() {
//code
CountDownTimer bCounter = new CountDownTimer(8000, 100) {
public void onTick(long millisUntilFinished) {
//update time
}
public void onFinish() {
title.setText(tempTitle);
}
};
bCounter.start();
stage3(8000); //another delay
};
}; handler2Mem.postDelayed(mem2Runnable, memTime);
}
}; handlerMem.postDelayed(memRunnable, 2500);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// this button checks stage#, if stage4 info - else...
}else{
aCounter.cancel();
onOffCDT = false;
cancelCountDown(mem2Runnable, handler2Mem);
}
}
});
}
private void stage3 (Integer tTime) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//Do Stuff
}
}, tTime);
}
public void cancelCountDown(Runnable run2, Handler hand2) {
hand2.removeCallbacks(run2);
}