如何以编程方式退出Android应用程序? [英] How to exit an Android app programmatically?

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

问题描述

我确定已经多次询问了这个问题,因为我读了几次.我的客户希望我在他的应用程序中放置一个按钮,用户可以单击并退出.我已阅读,发现调用finish()可以它.但是,完成只是关闭当前正在运行的活动,对吗?我有很多活动,因此在这种情况下,我必须传递每个活动的实例并完成它们,或将每个活动设置为Singleton模式.

I am sure this question has been asked number of times because I read a few. My client wants me to put a button into his app where users can click and exit. I have read this and found calling finish() will do it. But, finish is only closing the current running activity right? I have lot of activities so in this case I have to pass each and every activity's instance and finish them or make every activity into Singleton pattern.

我还知道Activity.moveTaskToBack(true)可以使您进入主屏幕.好的,这不是关闭过程,而是后台进程.那有效吗?

I also got to know Activity.moveTaskToBack(true) can get you into the home screen. OK, this is not closing but backgrounding the process. So is this is effective?

应该使用哪种方法完全关闭应用程序?上述任何方法或上述方法的任何其他方法/其他用法?

Which method should I use to close the app completely? Any of above described or any other method/other usage of above methods?

推荐答案

实际上,每个人都希望在onclick事件上关闭应用程序,无论它在哪里活动....

Actually every one is looking for closing the application on an onclick event, wherever may be activity....

因此,朋友们请尝试使用此代码.将此代码放在onclick事件

So guys friends try this code. Put this code on the onclick event

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
    homeIntent.addCategory( Intent.CATEGORY_HOME );
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
    startActivity(homeIntent); 

这篇关于如何以编程方式退出Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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