0

I have gone through many of the suggestion for this in stack and couldn't still resolve my issue. I was using a Dialog fragment TestFragment which is getting called from my AFragment class and the TestFragment is shown on top of the AFragment .Currently on clicking outside of the screen, I have to close the TestFragment. currently it is not getting closed. please find the below code. DialogFragment.getDialog().setCanceledOnTouchOutside(true); I'm getting the following error Non-static method 'getDialog()' cannot be referenced from a static context -> Couldnt resolve with this solution also. AFragment :

FragmentTransaction ft =    getChildFragmentManager().beginTransaction();
                   testfragment testfragments = new testfragment();
                   testfragments.getDialog().setCanceledOnTouchOutside(true);
                   ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
                   ft.addToBackStack("filterPriceFragment");
                   ft.replace(R.id.filter_pop_up_frame_lyt, testfragments, "filterPriceFragment");
                   ft.commitAllowingStateLoss();

TestFragment:

public class testfragment extends DialogFragment {


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {


        final Context contextThemeWrapper = new ContextThemeWrapper(getContext(), R.style.MyApplyButton);
        LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

        View view = localInflater.inflate(R.layout.filter_price_fragment_lyt, container, false);

        return view;

    }
}
sooraj
  • 41
  • 2
  • 8
  • https://stackoverflow.com/questions/8404140/how-to-dismiss-a-dialogfragment-when-pressing-outside-the-dialog – gtxtreme Sep 06 '21 at 01:52
  • Put it inside `onCreateView` as per the suggestion from above thread – gtxtreme Sep 06 '21 at 01:53
  • @gtxtreme - I have tried that also, when I put the same code on onCreateView like this `DialogFragment.getDialog().setCanceledOnTouchOutside(true);` I'm getting the following error `Non-static method 'getDialog()' cannot be referenced from a static context` – sooraj Sep 06 '21 at 01:56
  • You should add this in your question.`I couldn't resolve this issue` doesn't help us with what exactly your question is – gtxtreme Sep 06 '21 at 02:00
  • @gtxtreme - Updated the same above. – sooraj Sep 06 '21 at 02:04
  • Also did you try with `getDialog().setCanceledOnTouchOutside(true)`? – gtxtreme Sep 06 '21 at 02:04
  • @gtxtreme - let me try that now. – sooraj Sep 06 '21 at 02:04
  • @gtxtreme - `getDialog.setCanceltedOnTouchOutside(true)` throws an error for me . `java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Dialog.setCanceledOnTouchOutside(boolean)' on a null object reference` – sooraj Sep 06 '21 at 02:08
  • Have you used it in `onCreateView`? – gtxtreme Sep 06 '21 at 02:10
  • @gtxtreme - yes. `public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { final Context contextThemeWrapper = new ContextThemeWrapper(getContext(), R.style.MyApplyButton); LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); View view = localInflater.inflate(R.layout.filter_price_fragment_lyt, container, false); getDialog().setCanceledOnTouchOutside(true); return view;` – sooraj Sep 06 '21 at 02:11
  • @gtxtreme - any updates on this?? – sooraj Sep 06 '21 at 03:03
  • There are so many answers in the pointed link, why don't you try them out one by one? – gtxtreme Sep 06 '21 at 04:15
  • @gtxtreme - tried everything, not solving any of the solutions – sooraj Sep 06 '21 at 04:22

0 Answers0