English 中文(简体)
DialogFragment and Back button
原标题:DialogFragment and back button

是否有可能在<代码>DialogFragment上拦截关键纽子? 对该问题的担忧。 我的<代码>FragmentActective 从未被称作。

预先感谢

    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");
        }

令我感到惊讶的是,我补充说,第一号法典的用意是显示辩证。

最佳回答

很难说问题是什么,因为你没有公布任何法典。 但是,我的第一gues是,你通过打上<+BackStack<<> > 代码”将DialogFragment添入背部。 <代码>Fragmenttransaction ,经您重新使用,在活动中添加你的碎块。

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

由于你正在展示糖尿病,因此,创建的Dialog将接受主要活动,而不是家长的活动。 因此,建立了<代码>Dialog.OnKeyListener 。 当你制造Dialog碎块时,请在<代码>Dialog上打上<代码>Cancelable(false),以防止背后钥匙辞退。 然后,你可以处理您的<代码>中的后继钥匙。 OnKeyListener sonkey methods.

问题回答

• 采用最佳方法处理背顿的迪亚洛基金:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    return new Dialog(getActivity(), getTheme()){
        @Override
        public void onBackPressed() {
            // On backpress, do your stuff here.
        }
    };
}

https://stackoverflow.com/a/33915109/1650674> 如果你不使用建筑模式,Pundhir swer就可发挥很大作用。 如果你在你的方言中使用“造型”模式,你可以这样做:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                .setTitle(...)
                .setPositiveButton(...)
                .setNegativeButton(...)
                .setMessage(...)
                .create();

        alertDialog.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK 
                && event.getAction() == KeyEvent.ACTION_UP) {
                // TODO do the "back pressed" work here
                return true;
            }
            return false;
        });

        return alertDialog;
    }

这一工作首先分析了该系统如何使用<代码>onBackPressed()(对行动进行跟踪和倾听。 见。 The source on Dialog

you can use it for Dialog/ComponentDialog and BottomSheetDialog, just change the casted dialog with your type of dialog

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
       return super.onCreateDialog(savedInstanceState).also { dialog ->
        //change the ComponentDialog with your type of dialog
        (dialog as ComponentDialog).onBackPressedDispatcher.addCallback(this, object : 
            OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                // your code
            }
        })
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签