本文翻译自:How to quit android application programmatically

I Found some codes for quit an Android application programatically. 我找到了一些以编程方式退出Android应用程序的代码。 By calling any one of the following code in onDestroy() will it quit application entirely? 通过在onDestroy()中调用以下任何一个代码,它会完全退出应用程序吗?

  1. System.runFinalizersOnExit(true)
    (OR) (要么)
  2. android.os.Process.killProcess(android.os.Process.myPid());

I dont want to run my application in background after clicking quit button. 我不想在单击退出按钮后在后台运行我的应用程序。 Pls suggest me can i use any one of these code to quit my app? 请问我可以使用这些代码中的任何一个来退出我的应用程序吗? If so which code can i use? 如果可以,我可以使用哪些代码? Is it good way to quit the app in Android? 这是退出Android应用程序的好方法吗?


#1楼

参考:https://stackoom.com/question/qyM0/如何以编程方式退出android应用程序


#2楼

I think that application should be kill in some case. 我认为在某些情况下应用程序应该是kill。 For example, there is an app can be used only after login. 例如,有一个应用程序只能在登录后使用。 The login activity has two buttons, 'login' and 'cancel'. 登录活动有两个按钮,“登录”和“取消”。 When you click 'cancel' button, it definitely means 'Terminate the app'. 当您单击“取消”按钮时,它肯定意味着“终止应用程序”。 Nobody wants the app alive in the background. 没有人希望应用程序在后台运行。 So I agree that some cases need to shut down the app. 所以我同意某些情况需要关闭应用程序。


#3楼

There is no application quitting in android , SampleActivity.this.finish(); Android中没有应用程序退出 ,SampleActivity.this.finish(); will finish the current activity. 将完成当前的活动。

When you switch from one activity to another keep finish the previous one 当您从一个活动切换到另一个活动时,请完成前一个活动

Intent homeintent = new Intent(SampleActivity.this,SecondActivity.class);startActivity(homeintent);SampleActivity.this.finish();

#4楼

Is quitting an application frowned upon? 退出申请不赞成? . Go through this link. 浏览此链接。 It answers your question. 它回答了你的问题。 The system does the job of killing an application. 系统完成了杀死应用程序的工作。

Suppose you have two activities A an B. You navigate from A to B. When you click back button your activity B is popped form the backstack and destroyed. 假设您有两个活动A和B.您从A导航到B.当您单击后退按钮时,您的活动B将从后台堆叠中弹出并销毁。 Previous activity in back stack activity A takes focus. 后堆栈活动A中的先前活动需要关注。

You should leave it to the system to decide when to kill the application. 您应该将其留给系统来决定何时终止该应用程序。

public void finish() public void finish()

Call this when your activity is done and should be closed. 在您的活动完成后调用此选项并应关闭。

Suppose you have many activities. 假设你有很多活动。 you can use Action bar. 你可以使用动作栏。 On click of home icon naviagate to MainActivity of your application. 点击主页图标naviagate到你的应用程序的MainActivity。 In MainActivity click back button to quit from the application. 在MainActivity中单击后退按钮以退出应用程序。


#5楼

This may be very late and also as per the guidelines you're not supposed to handle the life cycle process by yourself(as the os does that for you). 这可能是非常晚的,并且根据指导原则,您不应该自己处理生命周期过程(因为操作系统会为您执行此操作)。 A suggestion would be that you register a broadcast receiver in all your activities with " finish() " in their onReceive() & whenever you wish to quit you can simple pass an intent indicating that all activities must shut down..... Although make sure that you do "unregister" the receiver in your onDestroy() methods. 建议您在所有活动中注册一个广播接收器,并在其onReceive()注册“ finish() ”。当您希望退出时,您可以简单地传递一个表明所有活动必须关闭的意图.....确保在onDestroy()方法中“取消注册”接收器。


#6楼

I'm not sure if this is frowned upon or not, but this is how I do it... 我不确定这是不是不赞成,但这就是我这样做的方式......

Step 1 - I usually have a class that contains methods and variables that I want to access globally. 第1步 -我通常有一个包含我想要全局访问的方法和变量的类。 In this example I'll call it the "App" class. 在这个例子中,我将其称为“App”类。 Create a static Activity variable inside the class for each activity that your app has. 在应用程序具有的每个活动的类中创建一个静态Activity变量。 Then create a static method called "close" that will run the finish() method on each of those Activity variables if they are NOT null . 然后创建一个名为“close”的静态方法, 如果它们不为null ,将对每个Activity变量运行finish()方法。 If you have a main/parent activity, close it last: 如果您有主/父活动,请将其关闭:

public class App{        // INSTANTIATED ACTIVITY VARIABLES            public static Activity activity1;        public static Activity activity2;        public static Activity activity3;        // CLOSE APP METHOD            public static void close()        {            if (App.activity3 != null) {App.activity3.finish();}            if (App.activity2 != null) {App.activity2.finish();}            if (App.activity1 != null) {App.activity1.finish();}        }}

Step 2 - in each of your activities, override the onStart() and onDestroy() methods. 第2步 -在每个活动中,重写onStart()onDestroy()方法。 In onStart() , set the static variable in your App class equal to " this ". onStart() ,将App类中的静态变量设置为“ this ”。 In onDestroy() , set it equal to null . onDestroy() ,将其设置为null For example, in the "Activity1" class: 例如,在“Activity1”类中:

@Overridepublic void onStart(){    // RUN SUPER | REGISTER ACTIVITY AS INSTANTIATED IN APP CLASS        super.onStart();        App.activity1 = this;}@Overridepublic void onDestroy(){    // RUN SUPER | REGISTER ACTIVITY AS NULL IN APP CLASS        super.onDestroy();        App.activity1 = null;}

Step 3 - When you want to close your app, simply call App.close() from anywhere. 第3步 -当您想关闭应用程序时,只需从任何地方调用App.close() All instantiated activities will close! 所有实例化的活动都将关闭! Since you are only closing activities and not killing the app itself (as in your examples), Android is free to take over from there and do any necessary cleanup. 由于您只是关闭活动而不是杀死应用程序本身(如您的示例中所示),因此Android可以从那里接管并进行任何必要的清理。

Again, I don't know if this would be frowned upon for any reason. 同样,我不知道这是否会因任何原因而不受欢迎。 If so, I'd love to read comments on why it is and how it can be improved! 如果是这样,我很乐意阅读有关它为什么以及如何改进的评论!

更多相关文章

  1. Android Studio 配置快捷方式生成JNI头文件的方法
  2. android 读取DDMS里的文件时打不开,解决方法
  3. Android Application Fundamentals——Android应用程序基础知识
  4. Android 应用程序只运行一个实例
  5. Android:Manifest merger failed with multiple errors, see log
  6. android中真正destroy掉activity的方法
  7. android中的按钮以图片的方式显示_基础篇
  8. Android旋转屏幕不销毁数据的方法

随机推荐

  1. How to store an image file to SQliteDB
  2. Android(安卓)6.0蓝牙读写和扫描权限问题
  3. android 使用Http的Get方式读取网络数据
  4. Android(安卓)逆向实战
  5. Android(安卓)真实 简历
  6. 更换android的初始化图片
  7. sdk2.2. 没发现有google map api 离线下
  8. Android学习札记20:ScaleGestureDetector
  9. Android中的骨架加载预览(Skeleton),Recycle
  10. java.lang.Class Cast Exception: androi