如何终止我自己的 Activity - 困难的方法

发布于 2024-09-10 05:54:43 字数 393 浏览 5 评论 0原文

所以我有了我的 Activity,按下“退出”按钮后,我调用 Activity.finish()。这有效地关闭了我的应用程序。

问题:我的应用程序的 Dalvik 进程仍然作为僵尸在后台徘徊。这似乎是正常的,因为其他应用程序也这样做。甚至 hello-world 示例都留在内存中。

我可以忍受这种情况,但不幸的是,这种行为使我的应用程序的开发变得痛苦。我有一个远程服务连接到我的活动,并且该服务在我的活动卸载之前不会卸载(正如所说的那样,它永远不会卸载)。

一切都以某种方式毫无理由地保持活力。

我怎样才能真正从内存中删除我的 Activity?

我正在寻找类似 Activity.finish_and_kill_my_process_please() 调用或类似的东西。

So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.

The problem: The Dalvik-process of my application is still hanging around as a zombie in background. It seems like this is normal as other applications do the same. Even The hello-world example hangs around in memory..

I could live with this, but unfortunatley this behaviour makes the development of my application a pain. I have a remote service connected to my Activity, and this service won't unload until my Activity unloads (which as said it never does).

Everything is somehow kept alive for no good reason.

How can I really remove my Activity from memory?

I'm looking for something like Activity.finish_and_kill_my_process_please() call or something similar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

贱人配狗天长地久 2024-09-17 05:54:43
  1. 在按钮单击时调用 finish();
  2. 将此行添加到 onDestroy 方法:

android.os.Process.killProcess(android.os.Process.myPid ());

    public void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());
    }       
  1. Call finish(); on button click
  2. Add this line to onDestroy method:

android.os.Process.killProcess(android.os.Process.myPid());

    public void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());
    }       
浅黛梨妆こ 2024-09-17 05:54:43

所以我有我的活动,并且按下
我称之为“退出”按钮
Activity.finish()。这有效地
关闭我的应用程序。

请不要这样做

我的应用程序的 Dalvik 进程
仍然像僵尸一样徘徊在
背景。

该进程保存在缓存中,供 Android 重用。您可以安全地忽略它。

我有一个远程服务连接到
我的活动,而该服务不会
卸载直到我的 Activity 卸载
(正如所说,它永远不会)。

您的应用程序中可能存在错误,例如调用 startService() 并未能调用 stopService(),或者调用 bindService() 并且无法调用 unbindService()

So I have my Activity, and on pressing
a "Quit" button I call
Activity.finish(). This effectively
closes my application.

Please don't do this.

The Dalvik-process of my application
is still hanging around as a zombie in
background.

The process is kept in a cache, for potential reuse by Android. You can safely ignore it.

I have a remote service connected to
my Activity, and this service won't
unload until my Activity unloads
(which as said it never does).

You probably have a bug in your application, such as calling startService() and failing to call stopService(), or calling bindService() and failing to call unbindService().

鹿港巷口少年归 2024-09-17 05:54:43

尝试使用 killBackgroundProcesses方法:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("name.of.your.package");

您需要添加 对您的 AndroidManifest.xml 文件的 KILL_BACKGROUND_PROCESSES 权限。

Try using the killBackgroundProcesses method:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("name.of.your.package");

You will need to add the KILL_BACKGROUND_PROCESSES permission to your AndroidManifest.xml file.

沙沙粒小 2024-09-17 05:54:43

通过终止进程,您可以完全停止应用程序。从按后退按钮的最后一个屏幕中,您可以通过以下方式终止进程:

public void onBackPressed() {   
        super.onBackPressed();   
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);    }

By killing the process you can stop the application completely.from the last screen on back button press you can kill the process by :

public void onBackPressed() {   
        super.onBackPressed();   
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);    }
江湖彼岸 2024-09-17 05:54:43

好吧,如果不仅是 Activity,还想终止应用程序,则可以使用

System.exit(0)

人们说它已被弃用或不好的示例,但是,它是纯 Java 并且确实有效...

Well, if not only activity, but also the application you want to terminate, you can use

System.exit(0)

People say that it's deprecated or bad example, but, well it's pure Java and does work...

疯狂的代价 2024-09-17 05:54:43

通过使用 ActivityManager,我们还可以关闭其他应用程序。

private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.jimmy.appToBeClosed");

要使用上述代码,应在清单文件中拥有 android.permission.RESTART_PACKAGE 权限。

By using the ActivityManager we can close the another applications also.

private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.jimmy.appToBeClosed");

to use the above code should have the android.permission.RESTART_PACKAGE permissions in manifest file.

_蜘蛛 2024-09-17 05:54:43

与 Java 端的 System.exit(0) 类似,您也可以在 onDestroy 期间调用本机方法,该方法在 C/C++ 端调用 exit(0)。这似乎迫使库完全退出。它对我有用!

Similar to the System.exit(0) on the Java side, you can also call a native method during onDestroy which calls exit(0) on the C/C++ side. This seems to force the lib to exit completely. It's working for me!

风为裳 2024-09-17 05:54:43

System.exit() 立即卸载整个虚拟机以及所有本机库、服务等。

System.exit() instantly unloads whole virtual machine with all native libraries, services and etc.

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