我应该先完成一项活动再进行另一项活动吗?

发布于 2024-12-13 08:55:26 字数 347 浏览 2 评论 0 原文

您是否总是在转到另一个活动之前对某个活动调用 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?

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

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

发布评论

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

评论(3

情深缘浅 2024-12-20 08:55:27

如果您了解 Android 应用程序的工作流程,则不需要覆盖后退按钮(除了一些特殊情况,例如游戏)。

如果您不希望用户返回到上一个 Activity,请完成它。应该不需要为此覆盖后退按钮。

public class Activity1 extends Activity{

    // Some onclick-Handler
    public void onButtonClick(View v){
        Intent i = new Intent(this, Activity2.class);
        this.startActivity(i);
        // Don't want you to return:
        this.finish();
    }
}

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.

public class Activity1 extends Activity{

    // Some onclick-Handler
    public void onButtonClick(View v){
        Intent i = new Intent(this, Activity2.class);
        this.startActivity(i);
        // Don't want you to return:
        this.finish();
    }
}
无名指的心愿 2024-12-20 08:55:27

如果您不希望后退按钮转到当前活动,请按后退按钮结束该活动。

如果打开了对话框,请覆盖活动的 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.

心的位置 2024-12-20 08:55:27

如果您想打开另一个活动并想完成前一个活动,请使用 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.

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