Reputation: 25
I have 3 activities(a,b and c). In activity a I have a countdowntimer which starts after some seconds activity b. In activity a the User can start activity c:
Intent myIntent = new Intent(a.this, c.class);
a.this.startActivity(myIntent);
finish();
with this code I want to finish the countdowntimer activity a and start c. Now the problem: When the user starts c, c starts but the countdown has not stoped and starts activity b when it has finished. Why? I have finsish the hole activity a - with the countdowntimer. I don't understand. I also tryed onDestroy()
, but it doesn`t works.
My Countdowntimer:
new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) { }
public void onFinish() { }
}.start();
Upvotes: 0
Views: 950
Reputation: 4005
public class a extends Activity {
CountDownTimer timer;
public void onCreate(Bundle bundle)
{
// ..
timer = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) { }
public void onFinish() {
Intent myIntent = new Intent(a.this, b.class);
a.this.startActivity(myIntent);
}
}.start();
}
public void startActivityC()
{
timer.cancel();
Intent myIntent = new Intent(a.this, c.class);
a.this.startActivity(myIntent);
}
}
Keep in mind that this is pseudocode and may have bugs/errors. Its simply intended to illustrate the technique rather than to be working code.
Upvotes: 2
Reputation: 1045
Override the onPause() function of activity a to stop the timer - maybe by checking a boolean variable set by whatever button or action starts the activity c.
Upvotes: 0
Reputation: 6159
when you start the new activity with a.this.startActivity(myIntent); the 'a' activity stops and the finish() call is never executed. You should stop the timer before starting the 'c' activity.
Upvotes: 0