How to stop CountDownTimer in android
You can assign it to a variable and then call cancel()
on the variable
CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {}
}.start();
yourCountDownTimer.cancel();
or you can call cancel()
inside of your counter scope
new CountDownTimer(zaman, 1000) {
public void onTick(long millisUntilFinished) {
cancel();
}
public void onFinish() {}
}.start();
Read more: https://developer.android.com/reference/android/os/CountDownTimer.html
Countdown timer doesn't stop [Android]
Try this
public final void starTimer() {
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String num = extras.getString("seg");
if(timerr!=null){
timerr.cancel();
timerr=null;
}
timerr = new CountDownTimer((Integer.parseInt(num)) * 1000 + 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished / 1000;
if (millis<10){
txtTempo.setText("0"+ (millisUntilFinished / 1000)) ;
} else {
txtTempo.setText("" + (millisUntilFinished / 1000));
}
}
@Override
public void onFinish() {
txtTempo.setText("00");
}
});
}
};
timerr.start();
}
Cancel countdowntimer if user navigates away from activity (Kotlin)
just set the CountDownTimer as val and the call the val.cancel()
create property
private lateinit var timer: CountDownTimer
set the val as CountDownTimer
timer = object : CountDownTimer(60000, 1000) {
override fun onFinish() {
val intent = Intent(applicationContext,Results::class.java)
startActivity(intent)
}
override fun onTick(p0: Long) {
time.text = ""+p0/1000
}
}.start()
set the timer.cancel() on backpress function
override fun onBackPressed() {
super.onBackPressed()
timer.cancel()
}
Cancelling a CountDownTimer on Physical Back Button Press in a Fragment
Here is my 2 cents. Fragment doesn't have an onBackPressed() method which is present in the Activity
class. It gets called when physical back button is pressed by the user. Here is what docs says:
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
What you can do is override the onBackPressed()
method in the parent activity of your Foo
fragment, then using an interface communicate to the fragment that back button was pressed by the user. Inside the fragment you can have the desired code to cancel the timer. This answer in the question How to implement onBackPressed() in Fragments? can help with sample code.
android - countdown timer is not canceling
Because you're calling cancel()
in onFinish
, the timer won't stop when the user clicks the button. What will happen instead is that the button will start a 5 second CountDownTimer
and at the end of the timer, cancel()
will be called. But what's the point of cancelling a timer when it's already finished?
To fix this, I'd suggest making a global instance of a CountDownTimer
object, instantiate it in the onCreate
method, and cancel it in the onClick
method.
First, add this to your global scope,
CountDownTimer timer;
Then, add what you originally had before in the load
method to your onCreate
,
timer = new CountDownTimer(5000, 10) { // adjust the milli
// seconds here
public void onTick(long millisUntilFinished) {
text.setText(""
+ String.format(
"%02d:%03d",
TimeUnit.MILLISECONDS
.toSeconds(millisUntilFinished)
- TimeUnit.MINUTES
.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(millisUntilFinished)),
TimeUnit.MILLISECONDS
.toMillis(millisUntilFinished)
- TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS
.toSeconds(millisUntilFinished))));
}
public void onFinish() {
text.setText("GameOver.");
//cancel(); <-this is redundant
}
}.start();
And call timer.cancel()
in your onClick
method,
public void onClick(View view) {
score++;
scoretv.setText(String.valueOf(score));
//load(); <-unnecessary
timer.cancel();
}
Lastly, I'd suggest getting rid of load
since it's sort of unnecessary at this point.
Problems To Cancel A CountDownTimer Android Java
Create a global object of CountDownTimer
eg.
On top of the main_activity class set: CountDownTimer timer;
after that do the things below.
timer = new CountDownTimer(11000, 1000)
{
public void onTick(long millisUntilFinished)
{
global.toast.setText("No Internet Connection!" + "\n" + "Automatic Refresh In: " + millisUntilFinished / 1000); //set text for toast
global.toast.show(); //show toast
}
public void onFinish()
{
if (network_connected == false)
{
global.cancel_toast(0); //stop all toasts
finish(); //quit activity
startActivity(new Intent(main_activity.this, main_activity.class)); //start activity
}
else
{
}
}
}.start(); //start the countdowntimer
}
and on onBackPressed
call timer.cancel();
like
@Override
public void onBackPressed()
{
if (page_number > global.page_number_min)
{ //does not matter
page_number--; //does not matter
global.cancel_toast(0); //stop all toasts
network_connected = true;
finish();
}
else
{
global.cancel_toast(0);
network_connected = true;
finish(); //quit activity
super.onBackPressed(); //quit application
}
timer.cancel();
}
Related Topics
Android Raw Folders - Creation and Reference
Set Language to French in Android Datepickerdialog
Android Sharedpreferences Limitations
Getting Null Device Id While Registering to Gcm
Failure [Install_Failed_Already_Exists] When I Tried to Update My Application
Estimating Beacon Proximity/Distance Based on Rssi - Bluetooth Le
How to Programmatically Log Out from Facebook Sdk 3.0 Without Using Facebook Login/Logout Button
Android Programming: Where to Start for Creating a Simple File Browser
Android - Google Play Like Tabs
Why Can't One Add/Remove Items from an Arrayadapter
How to Set Delay in Android Onclick Function
Android: How to Change the Datepicker View Date Format from Mm/Dd/Yyyy to Dd/Mm/Yyyy
Does the Android Emulator Support Opengl Es 3.0
How to Display Map (Google) on a Phonegap Android Application
How to Stop an Animation (Cancel() Does Not Work)
Android: How Handle Message Error from the Server Using Volley