Fragments should be static such that they can be re-instantiated by the system, and anonymous classes are not static

后端 未结 1 520
北海茫月
北海茫月 2021-01-15 09:36

The following code shows me the following error:

\"Fragments should be static such that they can be re-instantiated by the system, and anonymous classes are not stat

相关标签:
1条回答
  • 2021-01-15 10:30

    Read fragment life cycle You should use like this

    public static class MyAlertDialogFragment extends DialogFragment {
    
        public static MyAlertDialogFragment newInstance(int title) {
            MyAlertDialogFragment frag = new MyAlertDialogFragment();
            Bundle args = new Bundle();
            args.putInt("title", title);
            frag.setArguments(args);
            return frag;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int title = getArguments().getInt("title");
    
            return new AlertDialog.Builder(getActivity())
                    .setIcon(R.drawable.alert_dialog_icon)
                    .setTitle(title)
                    .setPositiveButton(R.string.alert_dialog_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((FragmentAlertDialog)getActivity()).doPositiveClick();
                            }
                        }
                    )
                    .setNegativeButton(R.string.alert_dialog_cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((FragmentAlertDialog)getActivity()).doNegativeClick();
                            }
                        }
                    )
                    .create();
        }
    }
    
    0 讨论(0)
提交回复
热议问题