[英]How to change arguments on detach/attach in a fragment?
我有一個從活動中調用的片段,該片段加載后會生成一些圖表。 現在,我必須更新那些圖表,但是用於更新那些圖表的數據來自該片段的參數。
public static BaseFragment newInstance(WholeReportsData wholeReportsDataFB, WholeReportsData wholeReportsDataTeam) {
MainReportFragment fragment = new MainReportFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_WHOLEREPORTSINFO_FB, wholeReportsDataFB);
args.putParcelable(ARG_WHOLEREPORTSINFO_TEAM, wholeReportsDataTeam);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@android.support.annotation.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mWholeReportsDataFB = getArguments().getParcelable(ARG_WHOLEREPORTSINFO_FB);
mWholeReportsDataTeam = getArguments().getParcelable(ARG_WHOLEREPORTSINFO_TEAM);
} else {
mWholeReportsDataFB = new WholeReportsData();
mWholeReportsDataTeam = new WholeReportsData();
}
}
我正在像下面的代碼一樣更新片段,但是我不知道如何更新參數。
private void reloadFragment(){
mReportsPresenter = ((HomeActivity)getActivity()).getReportPresenter();
mWholeReportsDataFB = new WholeReportsData(null, "", true);
mWholeReportsDataTeam = new WholeReportsData();
mReportsPresenter.requestReportsInfo(new ReportsRequestParams(getActivity(), ReportsRequestParams.TYPE_FAMILYBANKER));
if (SessionUserResult.getInstance().getFamilyBanker().isEsSupervisor()) {
mReportsPresenter.requestReportsInfo(new ReportsRequestParams(getActivity(), ReportsRequestParams.TYPE_TEAM));
mWholeReportsDataTeam.setLoadingService(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
getActivity().getSupportFragmentManager().beginTransaction().detach(this).commitNow();
getActivity().getSupportFragmentManager().beginTransaction().attach(this).commitNow();
} else {
getActivity().getSupportFragmentManager().beginTransaction().detach(this).attach(this).commit();
}
}
謝謝
您可以在活動和片段之間使用共享視圖模型並更新圖表,請選中此如何使用ViewModel在片段和活動之間進行通信
或者您可以檢查參數是否不為null,然后更新它們,否則設置新的參數,
Bundle bundle = new Bundle();
bundle.putLong(Constants.EXTRA_DO_ID, doId);
bundle.putInt(Constants.EXTRA_DO_OPTIONS_DIALOG_MODE, MODE_GET_INSPIRE_BY_DO);
if (fragment.getArguments() != null) {
fragment.getArguments().putAll(bundle);
} else
fragment.setArguments(bundle);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.