Android 应用程序退出按钮

发布于 2024-09-30 03:46:11 字数 81 浏览 1 评论 0原文

我不确定是否需要向我的应用程序添加退出按钮。这样做有什么意义吗?如果退出一项活动或服务时未 .finish() 或正确关闭,这是否会造成很大的损害?

I'm not sure whether I need to add an exit button to my app. Is there any point to doing this? And if on exiting one activity or service is not .finish() or closed properly could this cause a lot of damage?

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

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

发布评论

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

评论(10

我是有多爱你 2024-10-07 03:46:12

任何说你不需要退出按钮的人都是对的,但放一个退出按钮也没什么坏处。用户喜欢看到一种退出应用程序的方法,这与糟糕的编程无关,实际上与正确的编程有关,为用户提供一个退出按钮,如果他们从不使用它,那就更好了,如果它在那里就更好了。

Anyone who says you don't need an exit button is right but putting one in never hurts. Users like to see a way to exit to the application, it's not about bad programming, it's actually about proper programming, give the user an exit button and if they never use it great and if it's there even better.

嘴硬脾气大 2024-10-07 03:46:12

您的应用程序中不需要退出按钮。这就是安卓的工作原理。没有向用户提供任何实际退出应用程序的方法。

当您调用“完成”时,应用程序堆栈将被推送到后台。它仍然存在于记忆中。 Android 本身决定何时关闭应用程序(即从内存中删除其实例),通常这是在您的应用程序成为最旧的应用程序且最长时间未使用时完成的。

You don't need an exit button in your app. This is how android works. The user is not given any way to actually exit the application.

When you call 'finish', the application stack is just pushed to the background. It still exists in the memory. Android itself decides when to close the application(i.e. remove its instance from the memory) and generally this is done when your application becomes the oldest application which was not used for the longest time.

等你爱我 2024-10-07 03:46:12

不,你不知道。在developer.android.com 上阅读活动生命周期。

在旧版本的android 上,您必须小心,不要意外地让后台服务运行并持有资源,但这已经被重新设计了。

No, you don't. Read up on the activity lifecycle at developer.android.com

On older versions of android you had to be careful not to accidentally leave a background service running and holding resources but that's been reworked.

恰似旧人归 2024-10-07 03:46:12

伙计们,你是正义的,但在某些情况下,“退出”按钮可能是有感觉的。假设您在 YourApplication 类(Application 的子类)中保存一些应用程序范围的全局数据,例如一些敏感数据等。当用户关闭最终活动并且其被销毁时,应用程序不会终止。 作为阅读 Android 开发人员资源 - 开发人员不应依赖 Application.onTerminate() 方法。您可以轻松地检查它 - 即使所有活动关闭后应用程序仍然存在。只要应用程序实例处于活动状态,就可以相对轻松地访问这些对象并收集数据。

我找到了两种杀死 Application 类实例的方法:

  1. 将所有对象引用置空(这对于大型项目来说几乎不可能),然后调用垃圾收集
  2. call

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

因此退出按钮可能具有完成所有活动的功能,调用垃圾收集然后调用killProcess() - 这将保证安全删除存储在应用程序中的全局数据。

Guys you're righteous, but there are some cases when Exit button may have a sense. Let's say you're keeping some application-wide global data in YourApplication class (child of Application), e.g. some sensitive data or so. When user closes final activity and it's destroyed Application won't be terminated. As read Android developer resource - developer should not rely on Application.onTerminate() method. You can easily check it - even in case when all activities closed Application still alive. Whenever Application instance alive it's relatively easy to get access to those object and collect data.

I have found 2 ways how to kill Application class instance:

  1. null'ate all object references (which is hardly possible for large projects) then call garbage collection
  2. call

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

So Exit button may have function to finish all activities, call garbage collection then call killProcess() - which will guarantee safe removing of global data stored in Application.

娜些时光,永不杰束 2024-10-07 03:46:12

我同意 barmaley 的上述帖子。

长话短说,如果您的担忧与 Android 系统本身有关,则无需退出应用程序。别担心,开心就好。这尤其适用于懒惰的用户(还记得旧的 Symbian 时代)。

但是,如果您对应用程序本身有一些想法,请继续执行。你可以完成活动、无效化和杀死。保持理性,因为一如既往,常识将比世界上所有教程和参考资料更好。

无论如何,只是我的2c。

I agree with above post by barmaley.

Long story short, you don't need to exit your application if your concerns are related to the Android system itself. Don't worry, be happy. And that applies especially for lazy users (remember the old Symbian days).

However, if you have something in mind about your application itself, then please go ahead and do it. You can finish the activity, nullify and kill. Just be reasonable because, as always, common sense is going to be better than all the tutorials and references in the world.

Anyway, just my 2c.

深居我梦 2024-10-07 03:46:12

从技术角度来看,不,您不需要添加退出按钮,因为这里列出了所有很好的理由。

不幸的是,用户不会以这种方式得到通知,并且经常会降低评论分数,因为他们必须“使用任务杀手”或“强制关闭”您的应用程序。

这是我的应用程序评论/反馈中的常规请求和批评。所以可能有必要安抚用户。

From a technical perspective, no, you don't need to add a quit button for all the very good reasons listed here.

Unfortunately users aren't informed in this way and regularly drop review scores because they have to 'use a task killer' or 'force close' your app.

It is a regular request and criticism in reviews/feedback for my app. So it may be necessary to appease users.

肩上的翅膀 2024-10-07 03:46:12

我认为退出按钮可以向用户保证您的应用程序已关闭。

我知道,这仍然并不意味着应用程序肯定已关闭,但如果用户感觉通过退出按钮可以更好地控制,那么我说这是一个好主意。

I think an exit button can provide the user with assurance that your app is closed.

I know, that it still doesn't mean that the app is definitely closed, but if the user feels more in control with an exit button then I say it is a good idea.

月亮是我掰弯的 2024-10-07 03:46:12

当我第一次开始为 Android 编码时,我认为手动退出我的主要活动是个好主意。我发现通过 stopSelf 关闭 Activity 会导致实例状态数据损坏,从而在再次打开 Activity 时导致大量 ANR。

Activity Lifecycle文档是直接来自Google框架工程师的信息。每个词都是有原因的。

考虑一下垃圾收集会占用CPU。这可能看起来微不足道,但在为移动平台设计框架时,您是否会尝试在内存中缓存尽可能多的内容,以备再次需要时使用,因为 CPU 时间 = 电池,并且从闪存加载的成本很高?

对整个应用程序进行 GC 并非易事。特别是当该活动可能被重复使用时。设计必须考虑所有可能的用例。缓存是有道理的。本系统就是按照这个原理设计的。

我想说,不遵循活动生命周期文档就是自找问题。

When I first started coding for Android, I thought it was a good idea to manually exit my main activity. What I discovered is that closing the activity via stopSelf leads to corrupt instanceState data, which caused a lot of ANRs when the activity was opened again.

The Activity Lifecycle document is information directly from the Google framework engineers. Every word is for a reason.

Consider that garbage collection takes cpu. This may seem trivial, but in designing a framework for a mobile platform, wouldn't you try to cache as much in memory as possible in case it was needed again, as cpu time = battery, and loading from flash is expensive?

GCing an entire app is non-trivial. Especially when the activity might be used repeatedly. The design had to consider all possible use cases. Cacheing makes sense. The system is designed with this principle.

I would say that not following the Activity Lifecycle document is asking for problems.

╭⌒浅淡时光〆 2024-10-07 03:46:12

某些应用程序在运行时会更改设备的配置或消耗大量 CPU 或数据。您可能希望为用户提供完全退出应用程序的选项。

退出按钮有适当的用例。

例如,我制作的一个应用程序还需要管理WiFi网络:设备配置不断变化。太棒了,当应用程序处于活跃使用状态时,在这种情况下,用户会期望该行为在退出应用程序后有效停止。

所以我的应用程序有一个退出按钮,主要用于清理它创建的 WiFi 配置。我可以制作一个按钮“停止”和“清理”,但没有用户会理解。他们只是想关闭应用程序,从而退出。

  • 应用程序运行:由应用程序管理的wifi。
  • 应用程序退出:diy/android wifi。

现在,在我的应用程序中选择 Exit 会执行清理操作,然后执行 finish(),但应用程序本身会在后台徘徊。功能正常但让用户感到困惑。我可以做一个名为“wifi功能开/关”的切换动作,但这不会让事情变得更简单。

Some apps alter the configuration of the device while running or consume lots of CPU or data. You may want to give users an option to Exit the app altogether.

There are proper usecases for an Exit button.

For example, an app i made also needs to manage WiFi networks: the device configuration is changed continuously. Terrific and all while app is in active use, in this case a user would expect the behavior to effectively stop after exiting the application.

So my application has an Exit button mainly to cleanup the WiFi configs it created. I could make a button Stop and Cleanup, but no user would understand. They just want to turn the app off, thus Exit.

  • App running: wifi managed by app.
  • App exited: diy/android wifi.

Right now, choosing Exit in my application does the cleanup followed by a finish(), but the application itself lingers in the background. Functionally ok but confusing users. I could make a toggle action named "wifi feature on/off" but that wont make things simpler.

泪是无色的血 2024-10-07 03:46:11

您不需要添加退出按钮。如果不这样做,您的活动将保留在内存中,直到系统回收它。它不会消耗任何CPU。

当然,如果您有任何工作线程正在运行,您应该在 onStop() 或 onPause() 中停止它们,具体取决于您的线程正在做什么。

由于某种原因,许多应用程序添加了退出按钮。我猜原因是他们不相信自己能够编写正确的代码,或者不相信 Android 操作系统能够正确清理。

不执行正确的完成程序不会对您以外的其他人造成任何损害。

You don't need to add en exit button. If you don't, your activity will just be kept in memory until the system reclaims it. It will not consume any cpu.

Of course, if you have any worker threads running you should stop them in onStop() or onPause(), depending on what your threads are doing.

Many applications add an exit button, for some reason. I guess the reason is that they don't trust themselves to write proper code or they don't trust the Android OS to clean up properly.

Not going through the correct finish procedures will not cause any damage to others than yourself.

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