我有main activity
基本上托管2 fragments
。这些Fragments
中的每一个都有ListView
,其中包含一堆行。行上的OnItemClickListener
会打开一个包含详细数据的DialogFragment
。
在DialogFragment
我有一个按钮,然后在DialogInterface.OnClickListener positiveButtonOnClickListener
我要发布另一个包含此详细数据的片段。
从Fragment
启动DialogFragment
是否正确?
如果没有,我应该在Fragment
被解雇后从父片段启动此DialogFragment
。 (在这种情况下我有两个片段,其中任何一个都可以是父母)
或者我应该从MainActivity
启动它。(但是,从MainActivity
发起可能有问题,因为我不会从DialogFragment
启动MainActivity
MainActivity
没有数据,ArrayAdapter
的片段都有数据。
答案 0 :(得分:0)
每个Fragment
与FragmentManager
共享Activity
,因此可以使用Fragment
方法从DialogFragment
启动新的getFragmentManager()
答案 1 :(得分:0)
基本上,建议推出包括DialogFragment
在内的所有片段并从Activity
进行管理,因为Fragment
不可避免地需要上下文,但上下文仅来自Activity
。
启动Fragment
本身并不需要context
,但我说不可避免地,因为我们使用getString
或资源相关的代码需要context
}。
所以,在你的问题中,尽管很难做到,#3 是首选的选择。
如果ArrayAdapter
中有数据,您可以有两个选项。
重新设计您的数据层,以便从任何视图进行访问。存储库模式或Flux模式可能有助于重构应用程序。
只需实施#2 方式即可。这是处理DialogFragment
的最简单有效的方法。最好尽可能接近FragmentManager
来操纵Activity
。如果没有,ChildFragmentManager
会让人头疼。
#1 最简单,但在标签内部意味着它可以隐藏,因此可以进行垃圾回收。
答案 2 :(得分:0)
这是DialogFragment
的骨架示例,其中对话框包含Fragment
,并且还会弹出另一个DialogFragment
public class MyDialogFragment extends DialogFragment implements
MyFragmentOnTheDialog.MyFragmentOnTheDialogListener,
MyAdditionalPopupDialogFragment.MyAdditionalPopupDialogFragmentListener
{
对于对话框中的片段,它是动态添加的。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// fragment dynamically added
mFragmentManager = getChildFragmentManager();
// create a new instance of the fragment.
mMyFragmentOnTheDialog= MyFragmentOnTheDialog.newInstance();
// Add the fragment to the layout by replacing a frame on the layout
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.replace(R.id.frame_my_fragment, mMyFragmentOnTheDialog);
transaction.addToBackStack(null);
transaction.commit();
}
对于弹出式附加DialogFragment
MyAdditionalPopupDialogFragment ...
btnPick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction ft = mFragmentManager.beginTransaction();
MyAdditionalPopupDialogFragment frag = MyAdditionalPopupDialogFragment.newInstance();
// pass to the Dialog Fragment a reference to this container class
// in order to set up the interface between this fragment and that fragment.
// The reference to the MyAdditionalPopupDialogFragment is returned to 'frag'
frag.setMyAdditionalPopupDialogFragmentListener(EventAddFragment.this);
frag.show(ft, frag.TAG);
}
});
(您需要在其他DialogFragment
中添加setMyAdditionalPopupDialogFragmentListener来设置回调)
/**
* Sets the listener interface with the container class to allow a callback to be made from this
* fragment. The container class must implement the interface
* @param listener The container class that has implemented the MyAdditionalPopupDialogFragment interface
* @return A reference to this fragment
*/
public MyAdditionalPopupDialogFragment setMyAdditionalPopupDialogFragmentListener(MyAdditionalPopupDialogFragmentListener listener) {
mListener = listener;
return this;
}
重。 Youngjae的答案 - 这里context
没有问题。