我有一个Activity,我有一个MenuItem,单击它时会初始化以下内容DialogFragment:
MapDialogFragment df = new MapDialogFragment();
Bundle bundle = new Bundle();
bundle.putDoubleArray("lat_log", new double[] {48.85139, 2.34798});
df.setArguments(bundle);
df.show(fragmentManager, "google_maps_dialog_fragment");
这个 DialogFragment 里面有一个 GoogleMaps <fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"。
该GoogleMap对象有一个OnMapClickListener,当用户单击它时,会向它添加一个标记。一切都很好。
然后,用户单击关闭按钮,我运行以下代码:
getArguments().remove("lat_log");
dismissAllowingStateLoss();
然后Dialog关闭并Activity使用用户输入的数据(通过 a Listener)刷新我的代码。
MenuItem当用户再次单击并Dialog打开时,用户之前插入的每个数据仍然存在。
Dialog 的代码已在onCreateView()方法中初始化。我还没有实施onResume(),如果它有助于知道......
我究竟做错了什么?
非常感谢,费利佩
编辑1:
I have tried the following before showing the DialogFragment:
Fragment prev = fragmentManager.findFragmentByTag("google_maps_dialog_fragment");
if(prev != null) {
fragmentManager.beginTransaction().remove(prev).commit();
}
MapDialogFragment df = new MapDialogFragment();
Bundle bundle = new Bundle();
bundle.putDoubleArray("lat_log", new double[] {48.85139, 2.34798});
df.setArguments(bundle);
df.show(fragmentManager, "google_maps_dialog_fragment");
也没有工作...
有想法的人吗?
编辑2: 找到问题的根源:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.dialog, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
System.out.println("this is not working");
}
....
我添加了if (view != null)因为出于某种疯狂的原因,当我第二次实例化 DialogFragment 时,inflate 方法返回 null ......我不知道为什么。所以我保留了对viewas 类变量的引用......所以现在我必须找出为什么现在膨胀正在返回......
编辑 4
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
你去吧。这行得通!