Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intercept DialogFragment dismiss inside activity

I have an activity

public class ShowFileActivity extends FragmentActivity

and when occours some event, this class call a DialogFragment

public class ConfirmDialog extends DialogFragment

that is a simple confirm dialog (with "dismiss" and "ok" button). If user press dismiss button, i call

dismiss()

and come back to ShowFileActivity. Else, if user press ok, after made some operations, after call dismiss on dialog, i would go back to parent activity of ShowFileActivity. There's a way to do it? Does DialogFragment launch any event to his parent view?

like image 401
giozh Avatar asked Mar 26 '13 20:03

giozh


People also ask

How do you dismiss a DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do I know if DialogFragment is showing?

Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is DialogFragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


1 Answers

What you can do is to call a method of the containing activity from inside the fragment. As per any other fragment, you can call getActivity() which returns the containing activity.

@Override
public void onDismiss(DialogInterface dialog) {
    ShowFileActivity parent = (ShowFileActivity) getActivity();
    parent.doWhateverYouWantWhenDialogDismissed();
}

Another (more fancy) approach would be to use an event bus such as otto or greenrobot.

like image 99
fedepaol Avatar answered Nov 24 '22 09:11

fedepaol