1

I have followed this tutorial (4. Full-Screen Dialog) which is close to Material design guidelines.

But the interesting thing is, if I use the official code like this:

FragmentManager fragmentManager = getSupportFragmentManager();
FullscreenDialogFragment newFragment = new FullscreenDialogFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, 
newFragment).addToBackStack(null).commit();

I get:

enter image description here

But using the "simple" method of

DialogFragment dialog = new FullscreenDialogFragment();
dialog.show(getSupportFragmentManager(), TAG);

It works as intended:

enter image description here

So what's happening here?


My Code

My dialog class is basically the same except that I did not use this from the tutorial

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}

But instead I have a specified style file:

<!-- SA suggestion dialog theme. -->
<style name="FullscreenDialogTheme" parent="Base.Theme.AppCompat.DialogWhenLarge">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/colorLightBackground</item>
    <item name="android:windowIsFloating">false</item>

    <item name="android:windowEnterAnimation">@android:anim/slide_in_left</item>
    <item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
</style>

Which I set with

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NORMAL, R.style.FullscreenDialogTheme);
}

This is the dialog layout xml (I replaced the support widgets with AndroidX)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@android:color/white"
     android:fitsSystemWindows="true"
     android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/sa_dialog_close_btn"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:tint="@android:color/white"
            android:src="@drawable/ic_close_white_24dp"
            android:contentDescription="@string/sa_dialog_close_btn_content_desc" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="@string/dialog_title"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Title"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/sa_dialog_confirm_btn"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:text="@string/sa_dialog_confirm"
            android:textColor="@android:color/white" />

    </LinearLayout>

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:scrollingCache="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="Full Screen Dialog"/>

</androidx.core.widget.NestedScrollView>

And dialog class

public class FullscreenDialogFragment extends DialogFragment {

@BindView(R.id.sa_dialog_close_btn)
ImageButton close_btn;
@BindView(R.id.sa_dialog_confirm_btn)
Button confirm_btn;
//Necessary in fragments
private Unbinder unbinder;

Context mContext;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NORMAL, R.style.FullscreenDialogTheme);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_test, container, false);

 //        mContext = Objects.requireNonNull(container).getContext();
    unbinder = ButterKnife.bind(this, view);

    close_btn.setOnClickListener(listener -> cancelDialog());
    confirm_btn.setOnClickListener(listener -> confirmDialog());

    return view;
}

private void cancelDialog() {
    dismiss();
}

private void confirmDialog() {
//        Toast.makeText(mContext, "Confirmed", Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    unbinder.unbind();
 }
}

I guess it has something to do with this line, as suggested in this answer:
https://stackoverflow.com/a/44085523/1972372

transaction.add(android.R.id.content, fragment)

Where I should reference the layout from the activity that is creating the fragment?
I tried that with the root layout of the activity but only got weird content overlappings:

transaction.add(R.id.sa_activity_parent_layout, newFragment).addToBackStack(null).commit();

enter image description here

Community
  • 1
  • 1
Big_Chair
  • 2,781
  • 3
  • 31
  • 58

1 Answers1

1

there are some difference.show() method uses dialog as container, another one adds fragment to android.R.layout.content

wslaimin
  • 49
  • 2