简体   繁体   中英

Custom dialog in launcher activity overwriting activity layout

I have a dialog in my launcher activity that requires some user input and to display it I have this code in onCreate()...

 final Dialog dialog = new Dialog(this);
 dialog.setContentView(R.layout.dialog);
 DialogBinding dialogBinding = DataBindingUtil.setContentView(this, R.layout.dialog);
 popupBinding.dialogButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           dialog.dismiss();
       }
   });
 dialog.show();

The dialog button has an onClick listener to close it, but in order to do that, the dialog must be declared final. Previously (when this activity was not the launcher), I had the dialog declared as an instance variable, but this causes an error now.

private Dialog dialog = new Dialog(this); 

However, declaring the dialog in the onCreate method causes the launcher activity's layout to be replaced with the dialog's so that the dialog appears over a copy of itself.

带对话框的启动器活动

I'm not sure why it is doing that, but I was wondering if there is a way to prevent this. Thanks!

This accidentily sets the dialog layout as the content view for your activity.

DialogBinding dialogBinding = DataBindingUtil.setContentView(this, R.layout.dialog);

So you're mixing your activity with the dialog.

For your Activity, this should be something like

MyActivityBinding activityBinding = DataBindingUtil.setContentView(this, R.layout.my_activity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM