英文:
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?
我正在使用这段代码在1秒后强制关闭我的应用程序。但问题是它在关闭后重新启动了!如何使其永久关闭?
英文:
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)
}
答案1
得分: 1
使用以下方法强制退出您的应用程序:
fun exitApp() {
moveTaskToBack(true);
exitProcess(0)
}
exitProcess(status: Int): 终止当前正在运行的进程。
moveTaskToBack (boolean nonRoot):将包含此活动的任务移动到活动堆栈的后面。任务内的活动顺序保持不变。
或者
fun exitApp() {
finishAffinity()
}
finishAffinity():结束此活动以及当前任务中紧随其下具有相同关联性的所有活动。通常在应用程序可以启动到另一个任务(例如从其了解的内容类型的ACTION_VIEW)并且用户已使用向上导航从当前任务切换到自己的任务时使用。在这种情况下,如果用户已经进入了第二个应用程序的任何其他活动,所有这些活动都应作为任务切换的一部分从原始任务中移除。
英文:
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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论