您是否总是在转到另一个活动之前对某个活动调用 finish()
?
例如,为了防止用户通过移动后退按钮转到上一个活动,有些人建议您应该完成除主要活动之外的所有活动。这样,后退按钮始终会将您返回到主活动(或您认为用户应该导航的任何其他活动)。这是通过覆盖后退按钮行为来完成的。
不好的事情是,当处理程序运行一个对话框时,该对话框尝试在活动完成后运行(http://dimitar.me/android-displaying-dialogs-from-background-threads/)。
您对这个问题的经验法则是什么?以某种更智能的方式调用 finish() 或覆盖后退按钮以将用户引导至您选择的页面?
Do you always call finish()
on some activity before going to another activity?
For example, in order to prevent user going to the previous activity via mobile back button, some people suggest that you should finish all activities, except the main one. This way, the back button always returns you to the main activity (or any other activity you think a user should be navigated). This is done by overriding back button behaviour.
Bad thing of this is when there is a dialog run from the Handler which try to runs after the activity finished (http://dimitar.me/android-displaying-dialogs-from-background-threads/).
What is your rule of thumb on this issue? Call finish()
in some smarter way or overriding back button to direct user to page of your choice?
发布评论
评论(3)
如果您了解 Android 应用程序的工作流程,则不需要覆盖后退按钮(除了一些特殊情况,例如游戏)。
如果您不希望用户返回到上一个 Activity,请完成它。应该不需要为此覆盖后退按钮。
If you understood the workflow of an Android application, there should be no need to override the back-button (except for some special cases like Games for example).
If you don't want the user to get back to the previous Activity, finish it. There should be no need to override the back-button for that.
如果您不希望后退按钮转到当前活动,请按后退按钮结束该活动。
如果打开了对话框,请覆盖活动的 onPause 方法并关闭对话框。
当 Activity 离开屏幕时,将调用 onPause。
仅当我们需要执行特定操作时,我们才会覆盖 onBackPressed 方法,否则在正常情况下我们只需保留它即可。
If you do not want the back button to go to the current activity on back press finish the activity.
If you have a dialog opened, overide the onPause method of the activity and close the dialog.
onPause will be called when the activity goes offscreen.
We only overide the onBackPressed method when we need to do something specific otherwise in normal cases we just leave it.
如果您想打开另一个活动并想完成前一个活动,请使用 finish();调用另一个活动的意图后的函数。
它将完成当前活动并打开新活动。
If you want to open another activity and want to finish the previous activity then use the finish(); function after calling the intent of another activity.
it will finish the current activity and open the new activity.