我尝试了各种各样的解决方案,似乎无法在不使用廉价黑客(例如将数据保存到共享偏好等)的情况下找出实现此特定问题的最佳方法。
我的活动需要来自意图附加内容的数据:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_problem_set);
ButterKnife.bind(this);
this.problemSet = (ProblemSet) getIntent().getSerializableExtra("problemSet");
...
}
当用户点击问题集中的特定问题时,我产生了一个新的活动,它只是查看与问题集中特定问题相关的数据。注意,这只是一个数据视图(没有必要返回结果,因为没有返回任何内容)。
_problemsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Problem problem = ViewProblemSetActivity.this.problemSet.getProblems().get(position);
Log.d(TAG, "clicked on problem + " + problem.toString());
Intent problemFeedbackActivity =
new Intent(ViewProblemSetActivity.this, ProblemFeedbackActivity.class);
problemFeedbackActivity.putExtra("problem", problem);
startActivity(problemFeedbackActivity);
}
});
但是,当我单击返回并返回到现有的onCreate活动时,intent extras为null。在android中处理这种情况的最佳方法是什么?
我尝试了解决方案尝试,例如将额外内容保存到savedInstanceState但是当调用onCreate时,我的savedInstanceState仍为null:
Losing Intent extras when returning to Activity
将这些数据保存在onPause方法上更有意义吗?
Saving Android Activity state using Save Instance State
我实现了savedInstanceState功能,但在返回上一个活动时看起来仍然是相同的行为(空的Intent extras和null savedInstanceState)。我在第一个活动中添加了以下内容:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.d(TAG, "ViewProblemSetActivity.onSaveInstanceState savedInstanceState=" + savedInstanceState);
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putSerializable("problemSet", problemSet);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d(TAG, "ViewProblemSetActivity.onRestoreInstanceState savedInstanceState=" + savedInstanceState);
super.onRestoreInstanceState(savedInstanceState);
this.problemSet = (ProblemSet)savedInstanceState.getSerializable("problemSet");
}
@Override
protected void onResume() {
Log.d(TAG, "ViewProblemSetActivity.onResume");
super.onResume();
}
@Override
protected void onPause() {
Log.d(TAG, "ViewProblemSetActivity.onPause");
super.onPause();
}
这是从第一个活动创建开始,转到第二个活动,然后返回第二个活动的日志。
03-30 13:23:50.021 2540-2540/io.gradem.gradem D/view_ps_activity: ViewProblemSetActivity.onCreate savedInstanceState=null
03-30 13:23:50.026 2540-2540/io.gradem.gradem D/view_ps_activity: ViewProblemSetActivity.onResume
03-30 13:23:54.324 2540-2540/io.gradem.gradem D/view_ps_activity: clicked on problem
03-30 13:23:54.337 2540-2540/io.gradem.gradem D/view_ps_activity: ViewProblemSetActivity.onPause
03-30 13:23:54.354 2540-2540/io.gradem.gradem D/activity_feedback: ProblemFeedbackActivity.onCreate savedInstanceState=null
..... some over behavior before returning to previous activity ....
03-30 13:30:12.377 7266-7266/io.gradem.gradem D/view_ps_activity: ViewProblemSetActivity.onSaveInstanceState savedInstanceState=Bundle[{}]
03-30 13:23:58.861 2540-2540/io.gradem.gradem D/view_ps_activity: ViewProblemSetActivity.onCreate savedInstanceState=null
答案 0 :(得分:1)
savedInstanceState实际上应该解决问题,但使用单例类似乎更容易。