简体   繁体   English

如何使用onBackPressed()在片段上显示AlertDialog?

[英]How to display AlertDialog on Fragment with onBackPressed()?

I use DrawerNavigation to manage different fragments in the program and also override onBackPressed in the main activity to use the onBackPressed in fragment as this post: https://medium.com/@Wingnut/onbackpressed-for-fragments-357b2bf1ce8e 我使用DrawerNavigation来管理程序中的不同片段,还重写了main活动中的onBackPressed以使用片段中的onBackPressed作为这篇文章: https ://medium.com/@Wingnut/onbackpressed-for-fragments-357b2bf1ce8e

Thanks to Daniel Wilson that I can use onBackPressed in Fragment but when I want to display the alert when the user pressed back button after modifying an EditText without saving. 多亏了Daniel Wilson的帮助,我可以在Fragment中使用onBackPressed,但是当我想要显示警报时,用户在修改了EditText之后不保存就按下了back按钮。 The current fragment is just popped back to the previous one and the alert display on the other fragment. 当前片段仅弹出回到上一个片段,警报显示在另一个片段上。 How would I change it - Displaying the alert dialog on the dialog call? 如何更改-在对话框调用中显示警报对话框?

The following is my code 以下是我的代码

MyDialogFragment MyDialogFragment

public static class MyDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(getActivity())
                .setMessage(R.string.unsaved_changes_dialog_msg)
                .setPositiveButton(R.string.discard,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {

                                myContext.getSupportFragmentManager().popBackStack();
                            }
                        }
                )
                .setNegativeButton(R.string.keep_editing, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked the "Keep editing" button, so dismiss the dialog
                        // and continue editing the pet.
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                    }
                })
                .create();

        return alertDialog;
    }

}

onBackPressed onBackPressed

@Override
public void onBackPressed() {
    if (!mPetHasChanged) {
        Log.i(TAG, "onBackPressed: + mPetHasNotChanged");
        super.onBackPressed();
        return;
    }

    FragmentManager fm = getFragmentManager();
    MyDialogFragment dialogFragment = new MyDialogFragment ();
    dialogFragment.show(fm, "Sample Fragment");

    Log.i(TAG, "onBackPressed: ");
}

Thank you everyone, 谢谢大家,

Try this 尝试这个

@Override
public void onBackPressed() {

      FragmentManager fm = getFragmentManager();
      MyDialogFragment dialogFragment = new MyDialogFragment ();
      dialogFragment.show(fm, "Sample Fragment");

    super.onBackPressed();
    return;
   }

}

There is no onBackPressed() method for in Fragment, so we create CallBackInterface and use OnBackPressed() of activity. 在Fragment中,没有onBackPressed()方法,因此我们创建CallBackInterface并使用活动的OnBackPressed()。 Follow this link : http://vinsol.com/blog/2014/10/01/handling-back-button-press-inside-fragments/ hope It will help you. 请点击以下链接: http : //vinsol.com/blog/2014/10/01/handling-back-button-press-inside-fragments/希望对您有所帮助。

You can use OnBackPressed() of activity, when it calls, show your dialog. 您可以使用活动的OnBackPressed()进行调用,以显示对话框。 Add Listener on dismiss (use custom listener IMHO). 关闭时添加侦听器(使用自定义侦听器恕我直言)。 Dependence on the choice proceed your code. 取决于选择,继续执行您的代码。
It's like @mehul says, just add a listener mechanism. 就像@mehul说的一样,只需添加一个侦听器机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM