Search code examples
androidandroid-studioandroid-dialogfragment

Alert.Dialog builder code unreachable


I was trying to make a dialog Alert in android studio. But here it shows code is unreachable. Please help me out.

public class Mydialog extends DialogFragment
{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
    theDialog.setTitle("Sample Dialog");
    theDialog.setMessage("hello world");
    return super.onCreateDialog(savedInstanceState);
    theDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            toasty("Clicked Ok");
        }
    });



}

public void toasty(String str)
{

    Toast.makeText(getActivity(),str,Toast.LENGTH_SHORT).show();
}
}

Solution

  • Well, that's because you return super.onCreateDialog(savedInstanceState); before the end of the method. Put this line to the end of onCreateDialog().

    EDIT

    Sorry, I meant delete this line and add to the end:

    return theDialog.create();