I've tried a vast variety of solutions and can't seem to figure out the best way to implement this specific problem without using cheap hacks (such as saving data to sharedpreferences, etc.)
I have an activity that requires data from the intent extras:
@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");
...
}
When a user clicks on a specific problem within the problem set, I have a new activity that is spawned that simply just views data associated to a particular problem within the problem set. Note, this is just simply a view of the data (there is no need to start the activity for result as nothing is returned).
_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);
}
});
However, when I click back and return to the existing onCreate activity, the intent extras are null. What is the best way to handle this kind of situation in android?
I have tried solution attempts such as saving the extra to the savedInstanceState but when onCreate is called my savedInstanceState is still null:
Losing Intent extras when returning to Activity
Would persisting this data on the onPause method make more sense?
Saving Android Activity state using Save Instance State
EDIT:
I implemented the savedInstanceState functionality yet still seem the same behavior (empty Intent extras and null savedInstanceState) when returning to the previous activity. I added the following to the first activity:
@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();
}
Here is the log of beginning with the first activity create, going to the second activity, then returning to the second activity.
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