Reputation: 1782
I've implemented a fullscreen DialogFragment
exactly as it's done in the docs.
public void showDialog() {
CustomDialogFragment newFragment = new CustomDialogFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.add(android.R.id.content, newFragment).addToBackStack(null).commit();
}
public class CustomDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_items, container, false);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
// Code added her doesn't run on back press
}
}
Note: The onCancel()
and onDismiss()
functions run properly if I open the fragment using show()
, but that's not what I want here. Is this a bug or am I doing something wrong?
Upvotes: 0
Views: 218
Reputation: 1782
So I ended up overriding the dismiss()
and onDestroyView()
functions in my DialogFragment
to have the same behaviour while still opening it as a fragment instead of a dialog and it seems to works.
public class CustomDialogFragment extends DialogFragment {
boolean isDismissed;
OnCancelListener onCancelListener;
public CustomDialogFragment(@NonNull OnCancelListener onCancelListener) {
this.onCancelListener = onCancelListener;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_items, container, false);
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void dismiss() {
super.dismiss();
this.isDismissed = true;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (!this.isDismissed) {
this.onCancelListener.onCancel();
}
}
public interface OnCancelListener {
void onCancel();
}
}
Upvotes: 0
Reputation: 24
Function showDialog some problem, you can try this call show your custom dialog:
public void showDialog() {
CustomDialogFragment newFragment = new CustomDialogFragment();
newFragment.show(getSupportFragmentManager(), "show_dialog");
}
Text "show_dialog", you can replace text if you want
Edit: if you want full screen dialog, try this:
Add custom style:
<style name="full_screen_dialog">
<item name="android:windowIsFloating">true</item>
</style>
And set in CustomDialog:
public class CustomDialogFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_items, container, false);
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
}
@Override
public int getTheme() {
return R.style.full_screen_dialog;
}
@Override
public void onResume() {
super.onResume();
if (getDialog() != null) {
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes(params);
}
}
}
Upvotes: 0
Reputation: 217
Add this method to show your dialog in activity :-
public void showDialog() {
CustomDialogFragment newFragment = new CustomDialogFragment();
newFragment.show(getSupportFragmentManager(), "show_fragment");
}
Add this style :-
<style name="FullScreenfragment" parent="Theme.AppCompat.Light.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
Add this code in your CustomDialogFragment :-
@Override
public int getTheme() {
return R.style.FullScreenfragment;
}
Upvotes: 1