5

I've implemented a custom DialogFragment which I'm showing it on a RatingBar rating listener event in my postReviewFragment, I want to set my RatingBar rating back to 0 when the dialog is dismissed by the user.

Searching on SO I've come across these threads but the solutions doesn't seem to work for me :

DialogFragment Close Event

Can't use onDismiss() when using custom dialogs

DialogFragment and onDismiss

What I've tried so far using above threads :

Implementing DialogInterface.OnDismissListener on the postReviewFragment and overriding onDismiss() method

   @Override
    public void onDismiss(final DialogInterface dialog) {

        userRating.setRating(0);

    }

Also in the DialogFragment I've overide the onDismiss() method

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    final Fragment parentFragment = getParentFragment();
    if (parentFragment instanceof DialogInterface.OnDismissListener) {
        ((DialogInterface.OnDismissListener) parentFragment).onDismiss(dialog);
    }
}

But still the onDismiss() method does not fire up when the dialogFragment is dismissed, what am I doing wrong?

Community
  • 1
  • 1
RamithDR
  • 2,103
  • 2
  • 25
  • 34

2 Answers2

2

OK, I managed to figure it out :

  1. First of all make sure you've implemented DialogInterface.OnDismissListener and overriden onDismiss() method on both DialogFragment and the Fragment showing the dialog.

  2. Then when you show the DialogFragment set a target fragment, here I pass the rating via a Bundle to the DialogFragment

    android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
                PostReviewDialogFragment dialog = new PostReviewDialogFragment();
    
                // optionally pass arguments to the dialog fragment
                Bundle args = new Bundle();
                args.putInt("usersRating", rating);
                dialog.setArguments(args);
    
                dialog.setTargetFragment(RestaurantReviewFragment.this,REVIEW_FRAGMENT);
    
    
                dialog.show(fm, TAG);
    
  3. On DialogFraments's onDismiss() method I set the rating to 0 and add it to an intent extra and set ratingbar in dialogFragment to user selected value.

    Intent i = new Intent()
            .putExtra("rating1", rating);
    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
    
  4. Then I override onActivityResult() method to read the values and set ratingbar value on the fragment to 0 when dismissed.

This maybe not the best approach but it works, I will update answer if I find anything better than this method. Apologize if my explanation isn't very clear, check out below thread for more detailed explanation.

References : How to send data from DialogFragment to a Fragment?

Community
  • 1
  • 1
RamithDR
  • 2,103
  • 2
  • 25
  • 34
-1

Use the available OnDismissListener.

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    public void onDismiss(DialogInterface dialog) {
                        MainActivity.this.finish();
                    }
                });
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58