Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't force close app without it restarting?

I'm using this code to force close my app after 1 second. But the problem is that it restarts itself after closing! How can I make it close permanently?

  private fun endApp() {

    val handler = Handler()
    handler.postDelayed({ Process.sendSignal(Process.myPid(), Process.SIGNAL_KILL) }, 1000)
  }
like image 683
Bandy Avatar asked Dec 03 '25 10:12

Bandy


1 Answers

Use this method to force exit your app:

fun exitApp() {
    moveTaskToBack(true);
    exitProcess(0)
}

exitProcess(status: Int): Terminates the currently running process.

moveTaskToBack (boolean nonRoot) Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.

or

finishAffinity(): Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.

fun exitApp() {
    finishAffinity()
}
like image 142
Jorgesys Avatar answered Dec 05 '25 00:12

Jorgesys



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!