如何在Kotlin中关闭Android应用程序 [英] How to close Android application in Kotlin

查看:1129
本文介绍了如何在Kotlin中关闭Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JAVA中,我们可以关闭该应用程序.我们试图与Kotlin一起开发技能,并感到我们正在使用正确的语法关闭该应用程序.问题在于该代码仅在您在转到第二页并返回MainActivity之前关闭应用程序后才起作用,MainActivity是下面的启动器活动代码

In JAVA we can close the application. We trying to develop skills with Kotlin and feel we are using the correct syntax to close the application. The issue is that the code only works if you close the app before going to the Second Page and back to the MainActivity which is the launcher activity code below

    fun onTV(view: View){
    exitProcess()
}

private fun exitProcess() {
    //finish()
    System.exit(-1)
}

如果首先选择而不进行导航,则finish和System.exit(-1)均可工作 到PageTwoActivity

Both finish and System.exit(-1) work if selected first without navigating to PageTwoActivity

onTV是TextView的onClick属性 我的猜测是,我们需要清除堆栈购买设置标志,所以问题是 Kotlin的语法是什么?记住我们在启动器页面上 主要活动.我们需要结果的意图吗?

The onTV is the onClick property of a TextView My guess is that we need to clear the Stack buy setting Flags so the question is what is the syntax for this in Kotlin? Remember we are on the launcher page MainActivity. Do we need an Intent for results?

好吧,我尝试此代码没有任何改善

Ok I tried this code with no improvement

    val intent = Intent(context, MainActivity::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOIntent.FLAG_ACTIVITY_NEW_TASK
    finish()

我们现在关闭的是现在的代码,问题仍然是,如果您不导航到PageTwo并在启动后立即单击按钮以关闭应用程序,则它将关闭应用程序

We are close here is the code as it stands now the issue is still that while this will close the app if you do NOT navigate to the PageTwo and click the button to close the app right after it starts

    fun onTV(view: View){
    onBYE()
}

 fun onBYE() {
     exitProcess(-1)
 }

所以我想问题是执行onBYE时如何从堆栈中清除PageTwo吗?

So I guess the question is how to clear PageTwo from the stack when onBYE is executed ?

推荐答案

Grendel是关闭Kotlin应用程序的绝对最简单的两种方法 第一种方法是在重新加载时在PageTwo上打开应用程序 不优雅,但我包括有人有启动画面的机会

Grendel here is the absolute easiest two ways to close the Kotlin App the first way will open the app on PageTwo when it is reloaded not elegant but I included on the chance that someone has a Splash Screen

     moveTaskToBack(true);
     exitProcess(-1)

第二种方法是如此简单和古老,您会大喊大叫 它将关闭Kotlin应用程序,并在重新加载后首先显示MainActivity

The second way is so simple and old you are going to scream It will close the Kotlin App and when reloaded the MainActivity is shown first

finishAffinity()

我使用Nexus 9 API 26进行了测试,但没有三星Galaxy S2 但随时可以给我寄一封信,哈哈

I tested this with Nexus 9 API 26 I do not have a Samsung Galaxy S2 but feel free to mail me one ha ha

这篇关于如何在Kotlin中关闭Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆