Question

I want to finish my CountdownTimer on Back button. I know that there a a few similar questions but that doesn´t help me with my problem.

I´ve got the following code in onCreate()

        @Override
        public void onFinish() {



            new CountDownTimer(10000, 550) {

                @Override
                public void onTick(long millisUntilFinished) {
                    for(int i = 0; i< arr.size(); i++){
                        Button aga = arr.get(i);
                        if(aga.getVisibility() == View.VISIBLE){
                            aga.setVisibility(View.GONE);
                        }
                    }
                    int zufall = (int) (Math.random()*23);
                    setNextButton(arr.get(zufall));
                }

                @Override
                public void onFinish() {

                     System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

                    new CountDownTimer(10000, 350) {

                        @Override
                        public void onTick(long millisUntilFinished) {
                            for(int i = 0; i< arr.size(); i++){
                                Button aga = arr.get(i);
                                if(aga.getVisibility() == View.VISIBLE){
                                    aga.setVisibility(View.GONE);
                                }
                            }
                            int zufall = (int) (Math.random()*23);
                            setNextButton(arr.get(zufall));

                            System.out.println("HHH");
                        }

                        @Override
                        public void onFinish() {
                           System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                            Intent highscore = new Intent (MainActivity.this, Highscore_eintragen.class);
                            highscore.putExtra("count", count);
                            startActivity(highscore);


                        }
                    }.start();
                }
            }.start();
        }
    }.start();

Now what I need is, if the user presses the back button during the CountdownTimer to finish my activity. Any solutions for that problem?

Was it helpful?

Solution

Call CountDownTimer.cancel():

http://developer.android.com/reference/android/os/CountDownTimer.html#cancel()

store reference to current CountDownTimer instance in your activity during creation, to have access to it inside Activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top