I cannot finish activities with "finish()"... :(

790 Views Asked by At

I'm a beginner in android.

And I'm making an application about health care.

What I want to ask is how to finish an activity in the beneath case. The problem is the activity is not finished when I push the 'Back button' and 'ImageButton29'. To be specific, the countdowntimer still operates after I push those buttons... So, even if I go to next or previous pages(activity), after the countdown, the 'pushtaba2' activity is implemented. Please answer me.

package com.example.myapp

import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_pushtaba1.*
import android.content.Intent as Intent

class pushtaba1 : AppCompatActivity() {

    override fun onBackPressed() {
        finish()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pushtaba1)

        val intent = Intent(this, pushtaba2::class.java)

        val value = object : CountDownTimer(10000, 1000) {

            override fun onTick(millisUntilFinished: Long) {
                mTextField3.setText("" + millisUntilFinished / 1000)

            }

            override fun onFinish() {
                startActivity(intent)
                finish()
            }

        }.start()

        imageButton29.setOnClickListener {
            startActivity(intent)
            finish()
        }
    }
}
3

There are 3 best solutions below

0
Roshana Pitigala On

Override the fun onDestroy() in AppCompatActivity. This will be called whenever the Activity gets destroyed.

class pushtaba1 : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
    }

    override fun onDestroy() {
        // TODO - stop your timer here
    }
}
0
faysal neowaz On

you can use finishAffinity() or override onDestroy() and destroy the activity

0
Tonnie On

This is just to add to Roshana's Answers.

onDestroy() is only called when you call finish() on your activity or the System is temporary destroying the activity.

With this in mind you need to declare the timer as lateinit property so that it can be used inside onCreate() and onDestroy().

class pushtaba1 : AppCompatActivity() {
    
//declare timer here
    private lateinit var timer: CountDownTimer

    override fun onCreate(savedInstanceState: Bundle?) { ...}

I would also create a separate method to start the intent since you may need to the intent on a button click or when the timer elapses. Inside the method I call finish() to make sure onDestroy() is called to cancel the timer

private fun moveToNextActivity(){

        val intent = Intent(this@pushtaba1, pushtaba2::class.java)
        startActivity(intent)
        //make sure you call finish to force onDestroy() to be called
        finish()
    }

The next thing is to intilize and start the timer inside the onCreate() method.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

timer = object:CountDownTimer(10000,1000){
            override fun onTick(millisUntilFinished: Long) {
                //consider using String resources for this
                  mTextField3.setText("" + millisUntilFinished / 1000)
            }

            override fun onFinish() {
                moveToNextActivity()
            }
        
        }

//start the timer
        timer.start()

Lastly override the onDestroy() and stop the timer inside the onDestroy() method

override fun onDestroy() {
        super.onDestroy()
        //stopping the timer
        timer.cancel()
        
    }