退出Android应用程序的最佳方法是什么? [英] Best way to quit android app?

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

问题描述

我在寻找一种方式来退出我用code Android应用程序。 是的,我知道,我不应该做这项事业的android处理它,当你preSS后退按钮,但我有一个自定义的流程,强迫我实现这一点。我已经一直在寻找了一段时间,发现多个答案:

I'm looking for a way to quit my android app by code. Yes, I know, I shouldn't be doing this cause android handles it when you press the back button, but I have a customized flow that forces me to implement this. I've already been searching for a while, and found multiple answers:

  • 在它不应该做的=>无选项。
  • 在通话结束()在我的活动=>仅1活动,而不是整个应用程序。
  • 在开始的意图,第一个活动具有特殊标志=>我有多个入口点,所以不是一个真正的选择。
  • 杀了我自己的进程=>不知道这一个,这会调用所有活动中的onDestroy()方法?而将我的后台服务跟上这种运行? (这不应该被停止)

所以,我想知道,是否有任何其他选项,退出我的应用程序,还是我真的限于这些选项? 如果真的没有一个其他的选择的话,我想创造我的应用程序类的我活动的所有实例的列表,然后只需循环它们来调用finish()在他们身上。但我不知道这是否是正确的方式来处理这个问题。

So I'm wondering, is there any other option to quit my app, or am I really limited to these options? If there really isn't an other option, then I was thinking to create a list of all the instances of my activities in my Application class, and then just loop them to call finish() on them. But I don't know if this will be the correct way to handle this.

所以这就是为什么我问我的问题在这里,这是关闭我的应用程序的最佳方法是什么?

So that's why I ask my question here, which is the best way to close my app?

编辑:我​​或多或少地解决了这一问题。我有我的活动WeakReference的列表。在每一个的onCreate余的活性添加到列表中。然后,当我想放弃我的应用程序,我只是循环列表,并调用完成()。问题的存在:如果列表变得太大,它将不能完成我所有的活动,由于Android已经销毁了它们。所以每次我肯定知道我不需要他们了,我完成他们。在我的情况下,该列表无法生长大于3/4的活动,所以没有必要再担心没有得到完成的活动。同样用这种方法,我没有破坏我自己的过程。

I more or less fixed the problem. I have a list of WeakReference of my Activities. In every onCreate I add the activity to the list. Then, when I want to quit my app, I just loop the list and call finish(). Problem being: If the list gets too big, it won't finish all my activities, since android already destroyed them. So every time I know for sure I don't need them anymore, I finish them. In my case, the list can't grow bigger than 3/4 activities, so no need anymore to worry about activities not getting finished. Also with this method, I don't have to destroy my own process.

推荐答案

使用下面code。

android.os.Process.killProcess(android.os.Process.myPid());
    System.runFinalizersOnExit(true);

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

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