利弊通话结束
Pro and cons calling finish
我想知道当你尝试用 android 打开一个新的 activity 并通过调用 finish 直接销毁前一个时有什么优缺点。
人们认为这是个坏主意,因为 Android 可以处理 activity 并在内存使用过多时丢弃它们,但是如果我进入 activity 一次,用户可能永远不会回来?这是一个糟糕的选择吗?
此外,通过完成 activity,带有后退按钮的历史记录是 "clear",因此它不会返回到 activity(仅当您的用户流需要返回时我认为你不应该调用 finish )。
并且在内存方面,最好用 finish 杀死 activity 或留下 android 让这个 activity 在后台为 谁知道多长时间?
我觉得,您可以帮助系统对您关闭的 activity 进行 GC,并确保用户在退出应用程序之前不需要点击后退按钮 100 次。
那你怎么看呢?最好还是叫Finish
I want to know what are the pro and cons when you try to open a new activity with android and destroy the previous one straight away by calling finish.
要么您希望用户通过“后退”按钮return回到上一个activity,要么您不希望。
如果您希望用户通过 BACK return 到上一个 activity,请不要调用 finish()
如果您不希望用户通过 BACK return 回到上一个 activity,有多种选择,具体取决于您 do 希望用户在用户按下 BACK
时离开
People think that is a bad idea because Android can take care of the activity and drop them when there is too much memory used
is better to kill the activity with finish or leave android to have this activity in the background for who knows for how long time?
让 activity 实现 onTrimMemory()
并根据需要减少其内存占用是 "better"。不要伤害用户对后退按钮的期望。
that you kind of help the system to GC the activity that you closed and make sure that the user wont need to tap the back button 100 times before getting out of the application
很少有用户会 "tap the back button 100 times"。他们将按主页,或调出概览屏幕(a.k.a.,最近任务列表),或通过其他方式导航到另一个应用程序。
现在,话虽如此,将 有时您想要清除任务(返回堆栈),再次着眼于为用户提供逻辑流程。例如,在电子邮件应用程序中:
用户启动应用程序,并创建一个新任务,用户转到应用程序的启动器 activity,它显示用户收件箱 (A) 中的消息
用户点击 "search" 操作栏项目,调出搜索 activity,他们可以在其中按各种条件进行搜索 (B)
用户填写搜索条件并单击 "Go!" 按钮,执行搜索并显示匹配的电子邮件 (C)
用户点击电子邮件消息,弹出电子邮件查看 activity (D)
用户点击一个 "delete" 操作栏项目,这应该会删除消息并且 return 用户......某处
如果您认为用户应该 return 搜索结果 (C),您可以在 D 中调用 finish()
。但是,如果您认为用户应该 return 到收件箱 (A),您将使用适当的标志(例如,Intent.FLAG_ACTIVITY_CLEAR_TASK
)在 A 上调用 startActivity()
,以清除返回堆栈和 return 用户到 A.
总之:NOT调用finish()
来处理堆space;改为实施 onTrimMemory()
。但是,如果 导航 调用 finish()
,则使用它。
我想知道当你尝试用 android 打开一个新的 activity 并通过调用 finish 直接销毁前一个时有什么优缺点。
人们认为这是个坏主意,因为 Android 可以处理 activity 并在内存使用过多时丢弃它们,但是如果我进入 activity 一次,用户可能永远不会回来?这是一个糟糕的选择吗?
此外,通过完成 activity,带有后退按钮的历史记录是 "clear",因此它不会返回到 activity(仅当您的用户流需要返回时我认为你不应该调用 finish )。
并且在内存方面,最好用 finish 杀死 activity 或留下 android 让这个 activity 在后台为 谁知道多长时间?
我觉得,您可以帮助系统对您关闭的 activity 进行 GC,并确保用户在退出应用程序之前不需要点击后退按钮 100 次。
那你怎么看呢?最好还是叫Finish
I want to know what are the pro and cons when you try to open a new activity with android and destroy the previous one straight away by calling finish.
要么您希望用户通过“后退”按钮return回到上一个activity,要么您不希望。
如果您希望用户通过 BACK return 到上一个 activity,请不要调用
finish()
如果您不希望用户通过 BACK return 回到上一个 activity,有多种选择,具体取决于您 do 希望用户在用户按下 BACK
时离开
People think that is a bad idea because Android can take care of the activity and drop them when there is too much memory used
is better to kill the activity with finish or leave android to have this activity in the background for who knows for how long time?
让 activity 实现 onTrimMemory()
并根据需要减少其内存占用是 "better"。不要伤害用户对后退按钮的期望。
that you kind of help the system to GC the activity that you closed and make sure that the user wont need to tap the back button 100 times before getting out of the application
很少有用户会 "tap the back button 100 times"。他们将按主页,或调出概览屏幕(a.k.a.,最近任务列表),或通过其他方式导航到另一个应用程序。
现在,话虽如此,将 有时您想要清除任务(返回堆栈),再次着眼于为用户提供逻辑流程。例如,在电子邮件应用程序中:
用户启动应用程序,并创建一个新任务,用户转到应用程序的启动器 activity,它显示用户收件箱 (A) 中的消息
用户点击 "search" 操作栏项目,调出搜索 activity,他们可以在其中按各种条件进行搜索 (B)
用户填写搜索条件并单击 "Go!" 按钮,执行搜索并显示匹配的电子邮件 (C)
用户点击电子邮件消息,弹出电子邮件查看 activity (D)
用户点击一个 "delete" 操作栏项目,这应该会删除消息并且 return 用户......某处
如果您认为用户应该 return 搜索结果 (C),您可以在 D 中调用 finish()
。但是,如果您认为用户应该 return 到收件箱 (A),您将使用适当的标志(例如,Intent.FLAG_ACTIVITY_CLEAR_TASK
)在 A 上调用 startActivity()
,以清除返回堆栈和 return 用户到 A.
总之:NOT调用finish()
来处理堆space;改为实施 onTrimMemory()
。但是,如果 导航 调用 finish()
,则使用它。