当System.exit(0)执行时会发生什么?

发布于 2025-01-03 07:46:48 字数 536 浏览 1 评论 0原文

我有两项不同的活动。第一个启动第二个。

Intent intent = new Intent(this, Activity2.class);
startActivity(intent);

在第二个活动中,我调用 System.exit(0)。 我认为第一个活动是由“页面堆栈”引起的。 但我发现发生了两件事。

  1. 正在进行的变体失去了价值。 (我认为进度重新启动)
  2. 在第一个活动中创建的文件,并在第二个活动中附加数据并保存,丢失!(从沙箱中删除)。我使用 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.

  1. the variant in progress lost its value. (The progress restart I think)
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

望喜 2025-01-10 07:46:48

你可以做一件事:

不要使用 System.exit(0); 而你可以只使用 finish() 如下:

Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
finish();

这里数据不会丢失。HTH :)

You can do one thing:

Donot use System.exit(0); instead you can just use finish() as follows:

Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
finish();

Here data will not be loss.HTH :)

┊风居住的梦幻卍 2025-01-10 07:46:48

当System.exit(0)执行时会发生什么?

VM 停止进一步执行,程序将退出。

现在,在您的情况下,第一个活动由于活动堆栈而返回。因此,当您使用 Intent 从一个 Activity 移动到另一个 Activity 时,请像这样对当前 Activity 执行 finish() 操作。

Intent intent=new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
CurrentActivity.this.finish();

这将保证当我们关闭应用程序时没有任何活动正在运行。

要退出应用程序,请使用以下代码:

MainActivity.this.finish();          
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
getParent().finish();

如果您的应用程序在后台使用任何资源(例如从后台播放歌曲的音乐播放器或任何使用互联网数据的应用程序),则不应使用 System.exit()背景或任何取决于您的应用程序的小部件。

有关更多信息,请参阅以下参考资料:

  1. 退出应用程序会让人皱眉吗?
  2. http://android-developers.blogspot.in/2010/04/multitasking-android-way.html

What will happen when System.exit(0) execute?

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 the finish() of current activity like this.

Intent intent=new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
CurrentActivity.this.finish();

This will guarantee that no activity running when we close the application.

And for exiting from application use this code:

MainActivity.this.finish();          
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
getParent().finish();

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:

  1. Is quitting an application frowned upon?
  2. http://android-developers.blogspot.in/2010/04/multitasking-android-way.html
寻找我们的幸福 2025-01-10 07:46:48

那么,当 System.exit(0) 执行时到底发生了什么?

android.os.Process.killProcess(android.os.Process.myPid())System.exit(0) 是相同的。当您从第二个活动调用其中任何一个活动时,应用程序将关闭并仅使用一个活动再次打开(我们假设您有 2 个活动)。您可以通过将日志记录 (Log.i("myTag", "MainActivity started");) 放在主要活动的 onCreate 方法中来检查此行为。

So, what actually happened when System.exit(0) execute?

android.os.Process.killProcess(android.os.Process.myPid()) and System.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文