Android 如何使用前一个状态重新加载调用片段,并将得到的结果设置为从它启动的对话框片段回调的结果
这就是我的应用程序的运行方式。 应用程序有几个片段作为通过主活动连接的视图寻呼机。从第二个片段开始,我启动了CustomFragment对话框,在该片段对话框中单击ok按钮后,我将结果提取到主活动中。我正在使用android.support.v4库 这是我的MyDialog片段类,在这里我创建了一些行并给出了要选择的复选框Android 如何使用前一个状态重新加载调用片段,并将得到的结果设置为从它启动的对话框片段回调的结果,android,fragment,Android,Fragment,这就是我的应用程序的运行方式。 应用程序有几个片段作为通过主活动连接的视图寻呼机。从第二个片段开始,我启动了CustomFragment对话框,在该片段对话框中单击ok按钮后,我将结果提取到主活动中。我正在使用android.support.v4库 这是我的MyDialog片段类,在这里我创建了一些行并给出了要选择的复选框 private void performOKButtonFunctionality() { String msg = checkedBoxesCount + "";
private void performOKButtonFunctionality() {
String msg = checkedBoxesCount + "";
communicator.onDialogMessage(msg);
SecondFragment secondFragment = new SecondFragment();
secondFragment.testFunction(row);
dismiss();
}
现在,由于我很早就进入了android及其片段生命周期,我在实例化第二个片段后调用了一个方法。row参数只传递行号
现在在第二个片段类中,我有一些类似的东西
public void testFunction(String callBackRow) {
callBackRowNo = Integer.parseInt(callBackRow);
String callBackMessage = MainActivity.getCallBackMessage();
getTableData(callBackMessage + "");
}
为了保存SecondFragment的前一个状态,我通过如下方式使其保持静态来保存其上下文
private static Context activityContext;
public static SecondFragment newInstance(Context context) {
activityContext = context;
SecondFragment f = new SecondFragment();
Bundle b = new Bundle();
f.setArguments(b);
return f;
}
现在,在单击片段对话框中的ok按钮后,我只能使用第二个片段一次。但当再次单击时,应用程序在此时崩溃
FragmentManager myDialogManager = getActivity().getSupportFragmentManager();
引发的异常是:
现在,我的问题是:
我们如何重新进入片段的oncreate方法,在该方法中,它只是恢复其先前的状态,而不是在启动之前显式保存它
片段对话框
片段对话框如何将参数传递给可用于更新片段字段的片段。
有什么简单的方法可以做到这一点吗?
在询问之前,我搜索了SO,但没有找到询问或解释此场景的任何内容
我被困在这里,非常感谢任何解决方案/方向 尝试getChildFragmentManager而不是getSupportFragmentManager
当你用NewInstance制作fragment时,你可以这样放置参数
public static MyFragment newInstance(int value) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("value", value);
f.setArguments(args);
return f;
}
它还需要构造函数MyFragment
public MyFragment() {}
像这样从args获取值
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments() != null) {
value = getArguments().getInt("value");
}
return inflater.inflate(R.layout.your_layout, container, false);
}
public MyFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments() != null) {
value = getArguments().getInt("value");
}
return inflater.inflate(R.layout.your_layout, container, false);
}