AlertDialog with custom view in onCreate()

柔情痞子 提交于 2019-12-12 03:57:02

问题


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()?


回答1:


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



来源:https://stackoverflow.com/questions/12508747/alertdialog-with-custom-view-in-oncreate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!