DialogFragment和后退按钮 [英] DialogFragment and back button

查看:396
本文介绍了DialogFragment和后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能拦截在 DialogFragment 的按键?比较遗憾的是幼稚的问题。该 onBack pressed 我的 FragmentActivity 永远不会被调用。

Is there any possibility to intercept the key button in DialogFragment? sorry for the naive question.. the onBackPressed of my FragmentActivity is never called.

在此先感谢

    if (imageFile.exists()) {
            ShowPicDialog newFragment = ShowPicDialog.newInstance();
            FragmentTransaction ft = manager.beginTransaction();
            Fragment prev = manager.findFragmentByTag("picDialog");
            if (prev != null) {
                ft.remove(prev);
            }

            ft.addToBackStack("picDialog");
            newFragment.getArguments().putString("path", imageFile.getAbsolutePath());
            newFragment.show(ft, "picDialog");
        }

对不起,我加了code中的喀嚓我用它来显示对话框。

sorry I added the snip of code I use to show the dialog.

推荐答案

这很难肯定地说是什么问题,因为你还没有发布任何code。但是,我的第一个猜测是,你还没有通过调用 addToBackStack 方法添加的DialogFragment到后面堆了 FragmentTransaction 您正在使用您的片段添加到活动中。

It's hard to say for sure what the issue is, since you haven't posted any code. But my first guess is that you haven't added the DialogFragment to the back stack by calling the addToBackStack method of the FragmentTransaction that you're using to add your fragment to the activity.

有例子就在那给的<一个例子Android的文档页面href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentDialog.html">good图案中的活动使用DialogFragment。

There are examples right in the Android documentation pages that give examples of a good pattern for using a DialogFragment in your Activity.

由于正在显示一个对话框,该对话框创建将收到的关键事件,而不是父活动。因此,在设置了 Dialog.OnKeyListener 在创建对话的片段,并调用 setCancelable(假)对话框来prevent自贬返回键。然后,您可以处理在后面关键的 OnKeyListener onkey 方法。

Since you are displaying a Dialog, the created Dialog will receive the key events, not the parent Activity. So, set a Dialog.OnKeyListener when you create the Dialog's fragment, and call setCancelable(false) on the Dialog to prevent the back key from dismissing it. You can then handle the back key in your OnKeyListener's onkey method.

这篇关于DialogFragment和后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆