我有一个 FragmentActivity 支持 v4 类,它实现了两个并排的(有点像 gmail)片段和一个可以调出 DialogFragment 的按钮.
除非我改变方向,否则这一切都很好.当我更改方向时,不会保存片段的状态.
主要问题是如果我打开了 FragmentDialog,它就会消失.
我已经在所有片段中设置了 setRetainInstance(true); 但它没有帮助.
在我的清单中我包含了 android:configChanges="orientation" 但仍然没有帮助.
这是我的代码示例,感谢您的帮助.
public class AddMasterDialog extends DialogFragment {private int mTitle;私人 int mPrompt;私人 OnClickListener onSaveListener;私人 OnClickListener onCancelListener;public AddMasterDialog newInstance(int title, int prompt) {AddMasterDialog simpleDialog = new AddMasterDialog(title, prompt);返回 simpleDialog;}公共 AddMasterDialog() {//DialogFragment 所需的空构造函数}公共 AddMasterDialog(int 标题,int 提示){//DialogFragment 所需的空构造函数mTitle = 标题;mPrompt = 提示;}public void setSaveButton(OnClickListener save){onSaveListener = 保存;}public void setCancelButton(OnClickListener 取消){onCancelListener = 取消;}@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setRetainInstance(true);}@覆盖公共视图 onCreateView(LayoutInflater inflater, ViewGroup 容器,捆绑savedInstanceState) {//if(savedInstanceState == null){查看视图 = inflater.inflate(R.layout.add_new_simple, container);getDialog().setTitle(mTitle);((TextView) view.findViewById(R.id.add_simple_new_value_prompt)).setText(mPrompt);按钮 saveButton = (Button) view.findViewById(R.id.add_simple_save_button);saveButton.setOnClickListener(onSaveListener);//取消按钮按钮取消按钮 = (按钮) view.findViewById(R.id.add_simple_cancel_button);cancelButton.setOnClickListener(onCancelListener);返回视图;//}}}在我的主要活动中:
private void initialAddMasterPopupWindow() {尝试 {addMasterDialog = new AddMasterDialog(R.string.add_new_master_dialog_title, R.string.add_master_new_value_prompt);addMasterDialog.setSaveButton(saveNewMasterClickListener);addMasterDialog.setCancelButton(cancelNewMasterClickListener);FragmentManager fm = getSupportFragmentManager();addMasterDialog.show(fm, ADD_NEW_MASTER);} 捕获(异常 e){e.printStackTrace();}} 解决方案好的,问题似乎出在 DialogFragment 兼容性库上.
该问题在这篇帖子中有所描述.
片段的过时 DISMISS 消息保留在消息队列中.它在解除旧对话框时由 DialogFragment.onDestroyView() 排队,并在创建新对话框后重新激活.>
一个快速(也可能是肮脏的)解决方法是在调用 super.onDestroyView() 之前覆盖 onDestroyView() 并清除关闭侦听器"
将以下代码添加到我的 DialogFragment 解决了该问题:
@Override公共无效 onDestroyView() {if (getDialog() != null && getRetainInstance()) {getDialog().setDismissMessage(null);}super.onDestroyView();}I have a FragmentActivity support v4 class which implements two side by side (kind of like gmail) fragments and a button which can bring up a DialogFragment.
This all works great unless I have an orientation change. When I have an orientation change the states of the Fragments are not saved.
And the main problem is if I have a the FragmentDialog open, it simply disappears.
I have set setRetainInstance(true); in all fragments but it did not help.
In my manifest I included android:configChanges="orientation" but is still does not help.
Here are my code samples, thank you for the help.
public class AddMasterDialog extends DialogFragment { private int mTitle; private int mPrompt; private OnClickListener onSaveListener; private OnClickListener onCancelListener; public AddMasterDialog newInstance(int title, int prompt) { AddMasterDialog simpleDialog = new AddMasterDialog(title, prompt); return simpleDialog; } public AddMasterDialog() { // Empty constructor required for DialogFragment } public AddMasterDialog(int title, int prompt) { // Empty constructor required for DialogFragment mTitle = title; mPrompt = prompt; } public void setSaveButton(OnClickListener save){ onSaveListener = save; } public void setCancelButton(OnClickListener cancel){ onCancelListener = cancel; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //if(savedInstanceState == null){ View view = inflater.inflate(R.layout.add_new_simple, container); getDialog().setTitle(mTitle); ((TextView) view.findViewById(R.id.add_simple_new_value_prompt)).setText(mPrompt); Button saveButton = (Button) view.findViewById(R.id.add_simple_save_button); saveButton.setOnClickListener(onSaveListener); //Cancel Button Button cancelButton = (Button) view.findViewById(R.id.add_simple_cancel_button); cancelButton.setOnClickListener(onCancelListener); return view; //} } }and in my Main activity:
private void initiateAddMasterPopupWindow() { try { addMasterDialog = new AddMasterDialog(R.string.add_new_master_dialog_title, R.string.add_master_new_value_prompt); addMasterDialog.setSaveButton(saveNewMasterClickListener); addMasterDialog.setCancelButton(cancelNewMasterClickListener); FragmentManager fm = getSupportFragmentManager(); addMasterDialog.show(fm, ADD_NEW_MASTER); } catch (Exception e) { e.printStackTrace(); } }解决方案
Okay, so the issue seems to be with the DialogFragment compatibility library.
The issue was described in this post.
"An obsolete DISMISS message for the fragment is retained in the message queue. It's been queued by DialogFragment.onDestroyView() when dismissing the old dialog and gets reactivated after creating the new dialog.
A quick (and possibly dirty) workaround is to override onDestroyView() and clear the dismiss listener before calling super.onDestroyView()"
Adding the following code to my DialogFragment solved the issue:
@Override public void onDestroyView() { if (getDialog() != null && getRetainInstance()) { getDialog().setDismissMessage(null); } super.onDestroyView(); }
更多推荐
方向改变后Android DialogFragment消失
发布评论