当System.exit(0)执行时会发生什么?
我有两项不同的活动。第一个启动第二个。
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
在第二个活动中,我调用 System.exit(0)。 我认为第一个活动是由“页面堆栈”引起的。 但我发现发生了两件事。
- 正在进行的变体失去了价值。 (我认为进度重新启动)
- 在第一个活动中创建的文件,并在第二个活动中附加数据并保存,丢失!(从沙箱中删除)。我使用 applicationContext.openFileOutput(fileName, Context.MODE_PRIVATE); 创建的文件
在这种情况下是否已清理沙箱?通过“回车键”或什至通过 android.os.Process.killProcess(android.os.Process.myPid())
正常退出,沙箱中的文件被保留。 那么,当 System.exit(0) 执行时到底发生了什么?
谢谢!
I have two different activities. The first launches the second one.
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
In the second activity I call System.exit(0).
The first activity comes back caused by 'page stack' I think.
But I found two things happened.
- the variant in progress lost its value. (The progress restart I think)
- the file created in first activity, and appended data in second activity and saved, lost!(erased from sandbox). The file I created using
applicationContext.openFileOutput(fileName, Context.MODE_PRIVATE);
Was sandbox cleaned in that situation? The normal exit by 'return key' or even by android.os.Process.killProcess(android.os.Process.myPid())
, the file in sandbox was kept.
So, what actually happened when System.exit(0) execute?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以做一件事:
不要使用 System.exit(0); 而你可以只使用 finish() 如下:
这里数据不会丢失。HTH :)
You can do one thing:
Donot use System.exit(0); instead you can just use finish() as follows:
Here data will not be loss.HTH :)
VM 停止进一步执行,程序将退出。
现在,在您的情况下,第一个活动由于活动堆栈而返回。因此,当您使用
Intent
从一个 Activity 移动到另一个 Activity 时,请像这样对当前 Activity 执行finish()
操作。这将保证当我们关闭应用程序时没有任何活动正在运行。
要退出应用程序,请使用以下代码:
如果您的应用程序在后台使用任何资源(例如从后台播放歌曲的音乐播放器或任何使用互联网数据的应用程序),则不应使用 System.exit()背景或任何取决于您的应用程序的小部件。
有关更多信息,请参阅以下参考资料:
The VM stops further execution and program will be exit.
Now, in your case the first activity comes back due to activity stack. So when you move from one activity to another using
Intent
, do thefinish()
of current activity like this.This will guarantee that no activity running when we close the application.
And for exiting from application use this code:
And you should not use
System.exit()
if your application use any resource in background like Music player that plays song from background or any application that uses internet data in background or any widget that depends on your application.For more information go through the following references:
阅读文档:
http://developer.android.com/reference /java/lang/System.html#exit(int)
Read the documentation:
http://developer.android.com/reference/java/lang/System.html#exit(int)
android.os.Process.killProcess(android.os.Process.myPid())
和System.exit(0)
是相同的。当您从第二个活动调用其中任何一个活动时,应用程序将关闭并仅使用一个活动再次打开(我们假设您有 2 个活动)。您可以通过将日志记录 (Log.i("myTag", "MainActivity started");
) 放在主要活动的 onCreate 方法中来检查此行为。android.os.Process.killProcess(android.os.Process.myPid())
andSystem.exit(0)
are the same. When you call any of them from the second activity, then the application will be closed and opened again with only one activity (we assume that you had 2 activities). You can check this behaviour by putting logging (Log.i("myTag", "MainActivity started");
) in onCreate menthod of your main activity.