Android Fragments 当对话片段关闭时刷新片段

ercv8c1e  于 2023-08-06  发布在  Android
关注(0)|答案(7)|浏览(137)

有没有什么方法可以检测到DialogFragment何时被关闭,以便更新其父片段?

bvjxkvbb

bvjxkvbb1#

你可以添加一个监听器并覆盖片段对话框的onDismiss:

public class DismissDialog extends DialogFragment {
    private DialogInterface.OnDismissListener onDismissListener;

    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }

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

字符串
然后,在父节点上设置监听器:

DismissDialog d = new DismissDialog();
d.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {

        }
    });
d.show(getSupportFragmentManager(), "sometag");

7xllpg7q

7xllpg7q2#

重写DialogFragment的onDismiss(),或者在构建底层对话框时,使用setOnDimissListener()设置侦听器。

2w3rbyxf

2w3rbyxf3#

我试过@Gazer回答,它对我不起作用,我使用不同的片段类

import android.app.Fragment;

字符串
但我让它工作了
1.创建接口类

public interface MyDialogListener {
    void OnCloseDialog(Object obj); //you can put any object here
}


1.在父片段中实现接口类

public class ActionBarFragment extends Fragment implements MyDialogListener{

    @Override
    public void OnCloseDialog(Object obj) {
        //Do you refresh
    }


1.然后在DialogFragment中添加侦听器

public class SpecialDialogFragment extends DialogFragment { 

    MyDialogListener mListener;

    public SpecialDialogFragment(MyDialogListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onStop() {
        super.onStop();
        if(mListener!=null)
           mListener.OnCloseDialog(null);
    }

erhoui1w

erhoui1w4#

处理此问题的一种方法是将您的DialogFragment嵌入到Activity中,并将Activity显示为Dialog,以下链接中有一个技巧说明了如何操作:
http://developer.android.com/guide/topics/ui/dialogs.html
您可以使用它来更新底层的Fragment,因为当Dialog(它是一个Activity)完成时,将在底层的Fragment上调用onResume()。在onResume()方法中添加代码来更新片段的状态,这就是它的全部内容。

pkmbmrz7

pkmbmrz75#

我刚刚在我的项目中解决了这个问题。我有一个带有ListFragment的Activity。在该列表片段中,当单击特定项时,我将显示一个DatePickerFragment(一个显示DatePickerDialog的自定义DialogFragment)。选择日期后,我希望刷新ListFragment。
为了实现这一点,在承载ListFragment的Activity中(事实上,它也承载DatePickerFragment),我实现了一个方法,该方法只是用一个新示例替换ListFragment,如下所示:

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

字符串
我从DatePickerFragment调用这个方法,从onDateSet():

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


这对我来说很有用。

7d7tgy0s

7d7tgy0s6#

这里最受欢迎的答案都有问题。
DialogFragment中不能将侦听器作为示例变量保存,因为它们将无法在重新创建后继续存在,并且您会想知道为什么您的回调“神奇地”停止工作。
你可以在这里阅读更多关于这一点:
https://medium.com/@lukeneedham/listeners-in-dialogfragments-be636bd7f480
本文提供了几个真实的的解决方案,其中我最喜欢的是使用带有自定义接口的targetFragment。对于拦截dismiss,它看起来像:

class MainFragment : Fragment(R.layout.fragment_main), DummyDialogCallback {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        openDialogButton.setOnClickListener {
            val dialog = DummyDialog()
            dialog.setTargetFragment(this, DIALOG_DUMMY)
            dialog.show(requireFragmentManager(), "DummyDialog")
        }
    }

    override fun onDummyDialogClick() {
        Toast.makeText(requireContext(), "Dummy click", Toast.LENGTH_SHORT).show()
    }

    companion object {
        const val DIALOG_DUMMY = 1
    }
}

class DummyDialog : DialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = inflater.inflate(R.layout.dialog_dummy, container, false)

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        val callback = targetFragment as? DummyDialogCallback
        callback?.onDummyDialogClick()
    }
}

interface DummyDialogCallback {
    fun onDummyDialogClick()
}

字符串

vom3gejh

vom3gejh7#

正如@Luke提到的,最重要的答案有问题。您还可以使用FragmentLifecycleCallbacks实现您想要的功能,而无需任何自定义接口或侦听器。

1-创建内部FragmentLifecycleCallbacks()类:

// Create a FragmentLifecycleCallbacks (inner) class:
    inner class ChildFragmentCallbacks : FragmentManager.FragmentLifecycleCallbacks() {
         override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
            super.onFragmentPaused(fm, f)
            if(f is YourDialogFragment){
                // YourDialogFragment is closed, you can update parent fragment here
                updateParentFragment()
            }
        }
    }

字符串

2-在onCreate()上注册:

childFragmentManager.registerFragmentLifecycleCallbacks(ChildFragmentCallbacks(), false)


总代码:

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

class YourParentFragment : Fragment() {

    // ...

    // Create a FragmentLifecycleCallbacks (inner) class:
    inner class ChildFragmentCallbacks : FragmentManager.FragmentLifecycleCallbacks() {
         override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
            super.onFragmentPaused(fm, f)
            if(f is YourDialogFragment){
                // YourDialogFragment is closed, you can update parent fragment here
                updateParentFragment()
            }
        }
    }

    private fun updateParentFragment(){
      // Do it  
    }

    // Register it on onCreate():
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        childFragmentManager.registerFragmentLifecycleCallbacks(ChildFragmentCallbacks(), false)
    }

    // ...

}

相关问题