0

I have two main activities and the rest of the pages are full-screen DialogFragments (instead of another Activity), at some point, I'm opening a DialogFragment from another DialogFragment. How can the first DialogFragment know if pressed the back button or when the DialogFragment above it is dismissed?

I have not added the DialogFragments to the backstack because I don't want them to replace some view in the activity.

I tried to override the onResume() and onHidden(). I tried to add an event to the FragmentManager and none yielded any relevant results.

I would be glad if someone could shed some light on how I could do this?

My goal is to refresh the DialogFragment or Activity when it shows after the dialog above it is dismissed (new data from server or changes I made in the dialog that affect the previous page).

erc
  • 10,113
  • 11
  • 57
  • 88
asaf
  • 23
  • 1
  • 5

1 Answers1

0

You have a few options. The most straightforward way would be to just use an interface.

public class TopFragment {
    private OnDismissListener mListener;

    public void setOnDismissedListener(OnDismissListener listener) {
        mListener = listener;
    }

    private void dismiss() {
        mListener.onDismissed();
    }

    public interface OnDismissListener {
        void onDismissed();
    }
}

public class BottomFragment implements TopFragment.OnDismissListener {
    @Override
    public void onDismissed() {
        //reload views
    }

    private void createTopFragment() {
        TopFragment fragment = new TopFragment();
        fragment.setOnDismissedListener(this);
    }
}

However, if you are just worried about your data being in sync between screens you could also use something like a database ContentObserver, a BroadcastReceiver, or an event bus of some sort to listen for changes in your data and update your views accordingly. One of these is probably be a much better pattern because it decouples your two fragments whereas if you use a listener the two are highly coupled.

RJ Aylward
  • 843
  • 1
  • 7
  • 11