For fullscreen dialog, I check if an instance of it is already being shown at android.R.id.content
and then replace()
it:
public void showMyDialog(
String someArgument1,
String someArgument2
) {
MyDialog dialog = MyDialog.newInstance(
someArgument1,
someArgument2
);
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
Fragment content = getSupportFragmentManager().findFragmentById(android.R.id.content);
if (content instanceof MyDialog) {
// An instance of MyDialog is already displayed! Replace it
t.replace(android.R.id.content, dialog).commitAllowingStateLoss();
} else {
t.add(android.R.id.content, dialog).commitAllowingStateLoss();
}
}
And here is my custom fullscreen dialog with no title bar:
public class MyDialog extends AppCompatDialogFragment {
public static MyDialog newInstance(
String someArgument1,
String someArgument2) {
MyDialog dialog = new MyDialog();
Bundle args = new Bundle();
args.putInt("arg1", someArgument1);
args.putInt("arg2", someArgument2);
dialog.setArguments(args);
return dialog;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_dialog_layout, container, false);
// do whatever you want with the strings
// like displaying them in TextViews
String arg1 = getArguments().getString("arg1");
String arg2 = getArguments().getString("arg2");
return v;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}