Android Dialog not inflating view?

33 views Asked by At

In an Android App, I have a list of multiple entries, that should be editable. Since every entry is made out of multiple values, i've created a fragment that basically represents the edit-form.

When an element is clicked, that Fragment should be used to apply changes. So, I thought to use a Dialog to bring up the fragment, edit the entry, and save it if the dialog is dismissed.

While it all looks to be working, I just noted that the Java-Class associated with the Fragment that will be associated with the Dialog is not beeing inflated? Neither the Constructor nor OnViewCreated() is called when the dialog is shown.

I've also tried to manually inflate the view before attaching it to the dialog, but this doesn't work as well. Any thoughts?

SecondFragment.Java, bringing up the dialog on button press:

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ...
        addButton = this.getActivity().findViewById(R.id.button_add);
        ...

         addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog fbDialogue = new Dialog(getContext(), android.R.style.Theme_Black_NoTitleBar);
                View dialogView = getLayoutInflater().inflate(R.layout.fragment_button_dialog, null);
                fbDialogue.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(100, 0, 0, 0)));
                fbDialogue.setContentView(dialogView);
                fbDialogue.setCancelable(true);
                fbDialogue.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        DataProvider.storeJson(configuration, getActivity());
                        refreshButtonView();
                    }
                });
                fbDialogue.show();
            }
        });
    }

fragment_button_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonDialogFragment"
    android:fitsSystemWindows="true"
    android:fillViewport="true"
    >
    <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:layout_margin="15dp"
        android:padding="25dp"
        android:background="@color/cardview_dark_background">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/defineButtonHeader" />
...

and yeah, the associated ButtonDialogFragment isn't even constructed? (Breakpoints not hit by debugger)

1

There are 1 answers

0
dognose On

Got it working as expected with some changes:

  1. Rather than having a Fragment and attaching it to the Dialog, i've now changed the Fragment to be an actual DialogFragment.

  2. Logic to show the Dialog is then changed to

    addButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             ButtonDialogFragment bdf = new ButtonDialogFragment();
             bdf.show(getChildFragmentManager(), ButtonDialogFragment.TAG);
         }
     });
    
  3. Now it follows the intendet live-cycle for a DialogFragment.