单击按钮退出应用程序 [英] exit app on button click

查看:152
本文介绍了单击按钮退出应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在点击按钮时退出应用。 ??

解决方案

您应该阅读以下链接中找到的所有评论。



如何以编程方式退出Android应用程序 [ ^ ]


只需将此代码用于您的按钮:

 exitButton.setOnClickListener( new  OnClickListener(){

@覆盖
public void onClick(查看v){
// TODO自动生成的方法存根
finish();
System.exit( 0 );
}
});



虽然在Android应用上使用退出按钮不是一个好主意。让操作系统决定何时杀死您的活动。我应该首先推荐,了解 Android Activity生命周期,然后实现任何必要的回调。


Android的设计不赞成退出应用程序,而是管理它由操作系统。您可以通过相应的Intent调出Home应用程序:



 Intent intent =  new 意图(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(意图);





参考


I want to exit app on button click . ??

解决方案

You should read all of the comments on the thread found at the link below.

How to quit android application programmatically[^]


Just use this code for your button:

exitButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        finish();
        System.exit(0);
    }
});


Though it is not a good idea to use an exit button on an Android app. Let the OS decide when to kill your Activity. I should recommend, learn about the Android Activity lifecycle first and then implement any necessary callbacks.


Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);



reference


这篇关于单击按钮退出应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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