我有一个包含3个片段的视图寻呼机。我使用改装rest api来第一次填充我的每个碎片。我想要实现的是当用户向后滑动第一个或第三个片段(这2个片段正在被视图寻呼机销毁)以恢复数据(保存在数组列表中)而不再进行休息api调用。我所做的是将下载数据的数组列表保存在onSaveInstanceState()中,并在用户仅第一次刷回上述2个片段之一时成功检索它。问题是当我导航回到2个片段中的任何一个片段时,保存数组列表的特定束密钥包含空值。
CompletedSurveysFragment(第三个片段):
public class CompletedSurveysFragment extends Fragment implements SAMVCView {
private static final String debugTag = CompletedSurveysFragment.class.getSimpleName();
private View view;
private RecyclerView completedSurveysRcV;
private SAMVCPresenterImpl SAMVCpresenterImpl;
private SurveysRcvAdapter surveysRcvAdapter;
private List<SurveyData> data;
public CompletedSurveysFragment() {}
public static CompletedSurveysFragment newInstance() {
return new CompletedSurveysFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e(debugTag, "onCreateView");
if ( view == null ) view = inflater.inflate(R.layout.fragment_completedsurveys, container, false);
completedSurveysRcV = (RecyclerView) view.findViewById(R.id.completedSurveysRcV);
return view;
}
// TODO: 21/6/2016 configure Limit and offset values
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(debugTag, "onActivityCreated " + savedInstanceState);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
completedSurveysRcV.setHasFixedSize(true);
completedSurveysRcV.setLayoutManager(linearLayoutManager);
completedSurveysRcV.addItemDecoration(new DividerItemDecoration(ContextCompat.getDrawable(getActivity(), R.drawable.divider)));
if (savedInstanceState == null) {
SAMVCpresenterImpl = new SAMVCPresenterImpl(this);
SAMVCpresenterImpl.getSurveysBasedOnSpecificFirmId(new AllSurveysBody(getResources().getString(R.string.list_surveys), LoginActivity.getSessionPrefs(getActivity()).getInt(getResources().getString(R.string.firm_id), 0), getResources().getString(R.string.completed), 8, 0));
surveysRcvAdapter = new SurveysRcvAdapter(null, completedSurveysRcV);
completedSurveysRcV.setAdapter(surveysRcvAdapter);
} else {
//Log.e(debugTag, savedInstanceState.+"");
if (savedInstanceState.getParcelableArrayList("data") != null) {
Log.e(debugTag, "here "+ savedInstanceState);
surveysRcvAdapter = new SurveysRcvAdapter(savedInstanceState.<SurveyData>getParcelableArrayList("data"), completedSurveysRcV);
completedSurveysRcV.setAdapter(surveysRcvAdapter);
surveysRcvAdapter.notifyDataSetChanged();
}
//Log.e(debugTag, getArguments().getParcelableArrayList("data")+"");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) this.data);
Log.e(debugTag, "CompletedFragment onSaveInstanceState "+ outState);
}
@Override
public void onSuccessSurveysFetched(List<SurveyData> data) {
this.data = data;
surveysRcvAdapter = new SurveysRcvAdapter(data, completedSurveysRcV);
completedSurveysRcV.setAdapter(surveysRcvAdapter);
surveysRcvAdapter.notifyDataSetChanged();
}
@Override
public void onFailure() {
}
}
查看寻呼机适配器:
public class SurveysPagerAdapter extends FragmentStatePagerAdapter {
private static final String debugTag = SurveysPagerAdapter.class.getSimpleName();
private List<SurveyData> data;
String[] tabText;
public SurveysPagerAdapter(FragmentManager fragmentManager, String[] tabText) {
super(fragmentManager);
this.tabText = tabText;
}
@Override
public Fragment getItem(int position) {
Log.e("SurveysPagerAdapter", position+"");
switch (position) {
case 0:
return CompletedSurveysFragment.newInstance();
case 1:
return OngoingSurveysFragment.newInstance();
case 2:
return PendingSurveysFragment.newInstance();
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
return tabText[position];
}
@Override
public int getCount() {
return 3;
}
}
答案 0 :(得分:1)
您的问题是,您始终会返回片段的新实例。用户每次滑动时都不会调用CompletedSurveysFragment.newInstance();
(以及您的其他片段),而是创建一个片段数组并以这种方式检索它:
...
Fragment [] pages = new Fragment[getCount()];
...
@Override
public Fragment getItem(int position) {
Log.e("SurveysPagerAdapter", position+"");
switch (position) {
case 0:
if(pages[position] == null)
pages[position] = CompletedSurveysFragment.newInstance();
return pages[position];
...
}
然后,您可以在onCreate()
中获取和缓存数据,并稍后在相应片段中的onResume()
中检索它。
答案 1 :(得分:0)
发生这种情况的原因是因为onSaveInstanceState仅在父活动关闭时发生。由于当您在片段之间切换时,viewpager的父活动仍处于活动状态,因此不会调用onSaveInstanceState。
为了让数据像你想要的那样保存,我可能会做的是将数据保存在onPause()
中并检索onResume()
中的数据。
修改强>
根据docs,您需要在修改super.onSaveInstanceState(savedInstanceState);
后将来电置于Bundle
。
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
答案 2 :(得分:0)
我认为onPause()
和onResume()
无法奏效。片段与其父活动相关联,因此您浏览片段时父项活动处于onResume()
状态。我建议你做的是使用单例对象来获取和存储你的数据。每个片段都可以保留对相应对象的引用,并可以即时访问所需的数据。有关单身模式及其工作原理的大量信息。