我有一个 FragmentPagerAdapter,它通过自定义 SurveyPage 类生成的视图进行分页。寻呼机中的最后一页由 SurveyFinishPage 生成,它尝试访问与 FragmentPagerAdapter 关联的调查并将所有调查答案拉在一起以显示它们。SurveyFinishPage 通过主 Activity 中的 getSurvey() 方法获取 Survey 对象。
通常这可以正常工作,并且 SurveyFinishPage 能够访问 FragmentPagerAdapter 正在使用的同一个调查对象。但是,如果 Activity 已被杀死并恢复,SurveyFinishPage 似乎仍在访问旧的调查(或者可能是它的副本) - 它显示在 Activity 被丢弃之前的答案,无论对新的、重建的答案的更改如何民意调查。
似乎在我的 MainActivity 中的 onRestoreInstanceState() 中恢复 aSurvey 的值应该为其他所有内容建立调查对象,因为 MainActivity 将其调查对象传递给 FragmentPagerAdapter,后者使用该对象制作片段......但似乎FragmentPagerAdapter 和 SurveyFinishPage 正在寻找两个不同的东西。
如何正确恢复 FragmentPagerAdapter 中片段的状态,以便它们都引用同一个 Survey 对象?
MainActivity.java
public class MainActivity extends FragmentActivity {
SurveyPagerAdapter mSurveyPagerAdapter;
ViewPager mViewPager;
Survey aSurvey;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_poll);
if(savedInstanceState != null) {
testSurvey = savedInstanceState.getParcelable("survey");
}
else {
testSurvey = new Survey();
}
mSurveyPagerAdapter = new SurveyPagerAdapter(getSupportFragmentManager(), aSurvey);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSurveyPagerAdapter);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("survey", aSurvey);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
aSurvey = (Survey)savedInstanceState.getParcelable("survey");
}
[...]
}
SurveyPagerAdapter.java
public class SurveyPagerAdapter extends FragmentPagerAdapter {
private Survey survey;
[...]
@Override
public Fragment getItem(int i) {
Fragment fragment;
Bundle args = new Bundle();
// index 0 is the title page
if(i == 0) {
fragment = new SurveyTitlePage();
}
// final index is the finish page
else if(i == getCount()-1) {
fragment = new SurveyFinishPage();
}
// all other indices are regular poll pages
else {
args.putParcelable("question", survey.getQuestion(i-1));
fragment = new SurveyPage();
fragment.setArguments(args);
}
return fragment;
}
[...]
}
调查完成页面.java
public class SurveyFinishPage extends Fragment {
private Survey survey;
[...]
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setSurvey(((MainPoll)getActivity()).getSurvey());
LinearLayout view = new LinearLayout(getActivity());
view.setOrientation(LinearLayout.VERTICAL);
Button finishButton = new Button(getActivity());
finishButton.setText("FINISH");
final TextView textView = new TextView(getActivity());
finishButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String output = "";
for(int i=0; i<survey.getSize(); i++) {
try {
output += survey.getQuestion(i).getPrompt() + " - " + survey.getQuestion(i).getAnswer() + "\n";
} catch (Exception e) {
output += survey.getQuestion(i).getPrompt() + " - " + "no answer\n";
}
}
textView.setText(output);
}
});
view.addView(finishButton);
view.addView(textView);
return view;
}
}
调查页面.java
public class SurveyPage extends Fragment {
[...]
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle args = getArguments();
Question question = args.getParcelable("question");
View pageView = question.getQuestionView(getActivity(), container);
return pageView;
}
}