본문 바로가기

PROGRAMING/Android

DialogFragment


DialogFragment

http://developer.android.com/guide/topics/ui/dialogs.html


허니컴 (API11) 부터 Dialog를 생성할때는 사용하는 객체이다.


이전 버전 부터 존재하던 AlertDialog를 사용하여 UI를 생성하는 방법과

Fragment를  쓰는 것과 동일한 방법으로 UI를 생성하는 방법이 있다.



Basic Dialog


@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

            Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_dialog, container, false);

View tv = v.findViewById(R.id.text);



   /**

     * Create a new instance of MyDialogFragment, providing "num"

     * as an argument.

     */

    static MyDialogFragment newInstance(int num) {

MyDialogFragment f = new MyDialogFragment();


        // Supply num input as an argument.

        Bundle args = new Bundle();

        args.putInt("num", num);

        f.setArguments(args);


        return f;

    }



void showDialog() {

    mStackLevel++;


    // DialogFragment.show() will take care of adding the fragment

    // in a transaction.  We also want to remove any currently showing

    // dialog, so make our own transaction and take care of that here.

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    Fragment prev = getFragmentManager().findFragmentByTag("dialog");

    if (prev != null) {

        ft.remove(prev);

    }

    ft.addToBackStack(null);


    // Create and show the dialog.

    DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);

    newFragment.show(ft, "dialog");

}


Alert Dialog


@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();

    }



 public static MyAlertDialogFragment newInstance(int title) { 

 MyAlertDialogFragment frag = new MyAlertDialogFragment();

        Bundle args = new Bundle();

        args.putInt("title", title);

        frag.setArguments(args);

        return frag;

    }



void showDialog() {

    DialogFragment newFragment = MyAlertDialogFragment.newInstance(

            R.string.alert_dialog_two_buttons_title);

    newFragment.show(getFragmentManager(), "dialog");

}




show 함수 인자가 다른 두개가 존재한다.



void show(FragmentManager manager, String tag)

Display the dialog, adding the fragment to the given FragmentManager.


int show(FragmentTransaction transaction, String tag)

Display the dialog, adding the fragment using an existing transaction and then committing the transaction.