1

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);

}
Toclmi
  • 99
  • 2
  • 13

1 Answers1

0

simply add a if ((boolean)canceled)return in your method and set it to true when you want to cancel

Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
  • not following what you put, I'm running into not being able to set variables to these 2 pieces because they're not initialized. Using Final or public lets me use them in code but they're still not started as far as the code is concerned. – Toclmi Jun 12 '11 at 00:27
  • declare them in the class instead of in the method and set them to null. – Stas Jaro Jun 12 '11 at 01:33
  • Thanks for trying but what you're saying is obscure to me as I'm new to Android/Java. My understanding is that a method is basically a called set of code defined to preform "private void..." or provide a return 'private String/etc..." (Sub/Function from VB which I understand) Would a Class be an over-arching definition in the activity coding? Aren't my CountDownTimer and Handler/Runnable classes? do I have to define them using Class? This one thing is really annoying the crap out of me now wasting so much time trying to accomplish something almost inane which should be much simpler. – Toclmi Jun 12 '11 at 17:01
  • Post some of the code and I'll fix it for you. It should just take a sec. What your asking in your last comment is how does the basic syntax of java work which can be googled easily and would take me very long to explain. If you don't want to post your code here, just email he part that doesn't work to me – Stas Jaro Jun 12 '11 at 18:39
  • No worries, I think I understand what you're saying I was just trying to clarify. Method I'm pretty sure is called Sub or Function in VB, The class I was trying to determine if you meant the overarching class or if I should be creating a specific class for these delays. Also I edited the original post to include code. – Toclmi Jun 12 '11 at 20:26