Refresh fragment when dialogfragment is dismissed

前端 未结 6 1913
南方客
南方客 2021-02-04 01:01

Is there any way I can detect when a DialogFragment is dismissed, so that i can update its parent fragment?

6条回答
  •  心在旅途
    2021-02-04 01:40

    I just solved this in my project. What I have is an activity with a ListFragment. In that list fragment, when a particular item is clicked, I display a DatePickerFragment (a custom DialogFragment that displays a DatePickerDialog). When a date is selected, I want the ListFragment to be refreshed.

    To achieve this, in the activity that hosts the ListFragment (and it also hosts the DatePickerFragment, as a matter of fact) I implemented a method that simply replaces the ListFragment with a new instance, like so:

    public void criteriaChanged()
    {
        getFragmentManager().beginTransaction()
                .replace(R.id.container, new FilterFragment())
                .commit();
    }
    

    I call this method from the DatePickerFragment, from onDateSet():

    FilterActivity fa = (FilterActivity) getActivity();
    fa.criteriaChanged();
    

    This does the trick for me.

提交回复
热议问题