blyabtroi
blyabtroi

Reputation: 2076

AlertDialog with custom view in onCreate()

Here is my code:

    public class MainActivity extends Activity {

    private static final int CONFIRMATION_DIALOG = 0;
    private View mLoginConfirmView;
    private TextView mTextViewLoginConfirm;
    private CheckBox mCheckBoxLoginConfirm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLoginConfirmView = View.inflate(this, R.layout.dialog_login_confirmation, null);
        mTextViewLoginConfirm = (TextView) mLoginConfirmView.findViewById(R.id.textView_DialogTranspConfirm);
        mCheckBoxLoginConfirm = (CheckBox) mLoginConfirmView.findViewById(R.id.checkBox_DialogTranspConfirm);

        showDialog(CONFIRMATION_DIALOG);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case CONFIRMATION_DIALOG: {


            return new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.stat_sys_warning)
            .setTitle(R.string.app_name)
            .setView(mLoginConfirmView)
            .setPositiveButton(R.string.change, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    showDialog(EXIT_PROGRESS_DIALOG);
                }
            })
            .setNegativeButton(R.string.go_ahead, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    if (mCheckBoxLoginConfirm.isChecked())
                        setConfirmationDialogHidden();
                }
            })
            .create();
        }
            break;

        default:
            break;
        }
        return super.onCreateDialog(id);
    }

    @Override
    @Deprecated
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case CONFIRMATION_DIALOG: 

            CheckBox checkBox = (CheckBox)dialog.findViewById(R.id.checkBox_DialogTranspConfirm);
            checkBox.setChecked(false);
            TextView textView = (TextView)dialog.findViewById(R.id.textView_DialogTranspConfirm);
            textView.setText(getString(R.string.you_use_service_for, Utils.getNumber()));


            break;

        default:
            break;
        }
        super.onPrepareDialog(id, dialog);
    }
}

It crashes only on HTC One S, when my app returns from background on the line, where I set my custom view:

.setView(mLoginConfirmView)

Throwing: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. I've decided to add following check: if the view has a parent, I do nothing:

@Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case CONFIRMATION_DIALOG: {

        ViewGroup parent = (ViewGroup) mLoginConfirmView.getParent();
            if (parent!=null)
                return super.onCreateDialog(id);

            return new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.stat_sys_warning)
            .setTitle(R.string.app_name)
            .setView(mLoginConfirmView)
            .setPositiveButton(R.string.change, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    showDialog(EXIT_PROGRESS_DIALOG);
                }
            })
            .setNegativeButton(R.string.go_ahead, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    if (mCheckBoxLoginConfirm.isChecked())
                        setConfirmationDialogHidden();
                }
            })
            .create();
        }
            break;

        default:
            break;
        }
        return super.onCreateDialog(id);
    }

Is it correct way? Can I show dialogs from onCreate()?

Upvotes: 0

Views: 1439

Answers (1)

Hardik4560
Hardik4560

Reputation: 3220

It will crash as the UI of the activity is not yet prepared, and activity is not yet shown to the user, android shows the screen after its onResume() execution is completed, try showing the dialog from onResume()

Upvotes: 2

Related Questions