Dialogfragment and Ondismiss

How to get reason for Android DialogFragment onDismiss() being called?

If the users dismiss the dialog by pressing BACK or touching the area outside of the dialog window, onCancel() will be called before onDismiss().

If the users dismiss the dialog by clicking a Button, you will be able to catch the click event.

So you can solve the problem by overriding onCancel() instead of onDismiss() and keeping track of the click events.

DialogFragment OnDismissListener not being called

As per the onCreateDialog documentation:

Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself. To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).

And this has always been the case.

You should create your own callback interface and call that from your DateDialog's onDismiss() rather than calling setOnDismissListener directly:

class DateDialog {
interface DismissListener {
void onDismiss(boolean hasDate)
}

private DismissListener listener = null;

public void setDismissListener(DismissListener listener) {
this.listener = listener;
}

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (listener != null) {
listener.onDismiss(HasDate);
}
}
}

Then use it like:

 final DateDialog d = new DateDialog();
d.show(getChildFragmentManager(), "DateDialog");
getChildFragmentManager().executePendingTransactions();
d.setDismissListener(new DismissListener() {
@Override
public void onDismiss(boolean hasDate) {
if (hasDate) {
DoStuff();
}
}
});

How to listen to a DialogFragment dismiss event

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?

ANDROID - setOnDismissListener on a DialogFragment

You can override the onDismiss(DialogInterface dialog) method, which will be called when the DialogFragment is dismissed. You can also do it from your activity with an inline override:

public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment() {
@Override
public void onDismiss(DialogInterface dialog){
// Add your code here
}
};
newFragment.show(this.getFragmentManager(), "timePicker");
}

Getting information from DialogFragment using onDismiss()

Use a custom listener, below is an example on how this could be implemented. This is also explained in the Android Developer Guide.

public class CustomDialog extends DialogFragment {

public interface CustomListener{
void onMyCustomAction(CustomObject co);
}

private CustomListener mListener;

public void setMyCustomListener(CustomListener listener){
mListener = listener;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
Code to create dialog
...
}

@Override
public void onDismiss(DialogInterface dialog) {
if(mListener != null){
CustomObject o = new CustomObject();
mListener.onMyCustomAction(o);
}
super.onDismiss();
}
}

And when the custom dialog is created, set the listener.

CustomDialog awesomeDialog = new CustomDialog();
awesomeDialog.setMyCustomListener(new CustomDialog.CustomListener() {
@Override
public void onMyCustomAction(CustomObject o){
Log.i("TAG",o.toString());
}
});

How to set onDismissListener of DialogFragment in MainActivity?

Make your Activity implement OnDismissListener

public final class YourActivity extends Activity implements DialogInterface.OnDismissListener {

@Override
public void onDismiss(final DialogInterface dialog) {
//Fragment dialog had been dismissed
}
}

DialogFragment already implements OnDismissListener, just override the method and call the Activity.

public final class DialogFragmentCustom extends DialogFragment {

///blah blah

@Override
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
final Activity activity = getActivity();
if (activity instanceof DialogInterface.OnDismissListener) {
((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
}
}
}

Try this out.

DialogFragment timepicker onCancel and onDismiss problem

It's actually simpler than I thought all you need to do is to add a dismiss listener and change your ui/logic accordingly

TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

}
}, 10, 10, true);

dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(MainActivity.this, "dismissed", Toast.LENGTH_LONG).show();
}
});

dialog.show();

You can use the standard TimePickerDialog

Edit: Put everything to onCreate() except for dialog.show(), dialog.show() goes into the ifChecked in the Switch



Related Topics



Leave a reply



Submit