0

I'm kind of new to Android development and I encounter a problem here.

I want to launch an activity (another Class) when my countdown is over.

Edit:

Eclipse is telling me the code won't work, underlining part of my code in red.

The problem doesn't come from the Manifesto or such, I just can't figure out how to implement the call of my other activity.

/Edit

Here is my code:

public static void launch_countdown(final TextView tv1){
    new CountDownTimer(30000, 1000) {

         public void onTick(long millisUntilFinished) {
             tv1.setText("" + millisUntilFinished / 1000);
         }

         public void onFinish() {
             /* I want to launch my Activity here */
         }
    }.start();
} 

So basically, what should I replace my comment by ?

PS: I made some research but it doesn't seem to work in my situation:

How to call an activity from a CountDownTimer?

Android finish Activity and start another one

How to start an activity upon the completion of a timer?

Community
  • 1
  • 1
tcollart
  • 1,032
  • 2
  • 17
  • 29

1 Answers1

1

If you're creating this CountdownTimer inside an activity, you should be able to do this:

public void onFinish() {
    Intent i = new Intent(getApplicationContext(), OtherActivity.class);
    startActivity(i);
    finish(); // if you want this activity to go away
}

(If you like, you can replace getApplicationContext() with CurrentActivity.this. You cannot use a bare this because that refers to current instance of the anonymous CountDownTimer subclass to which onFinish() belongs.)

If this code is in some other class, you'll need access to a Context for the first argument. For instance, you could use tv1.getContext().

Alternatively, you can use one of the other Intent constructors that does not require a Context. See the API docs for more info.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521