I have a blog app and I'm having a hard time with my PostFragment.
When user is posting, he can select a feeling by clicking on a button which takes him to another fragment with the feelings list. Once he selects the feeling, I call popBackStack() in order to take him to the previous screen.
As I need to display his selection on the PostFragment, I'm saving that value in the Activity (I saw this technique here in SO). This is the activity's selectedFeeling variable:
public class FeedActivity extends BaseActivity {
Intent intent;
Post post;
private Feeling selectedFeeling;
@Override
protected void onCreate(Bundle savedInstanceState){
...
And the listener on FeelingsFragment:
private AdapterView.OnItemClickListener onGridViewItemClick() {
return new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Feeling selected = feelings.get(position);
((FeedActivity)getActivity()).setSelectedFeeling(selected);
getActivity().getSupportFragmentManager().popBackStack();
}
};
}
It is all working quite well: User taps the feelings button, goes to FeelingsFragment, selects the feeling, I save it to the activity, call popBackStack() and display the selected feeling in my PostFragment on the OnResume method:
@Override
public void onResume() {
super.onResume();
selectedFeeling = ((FeedActivity)getActivity()).getSelectedFeeling();
if(selectedFeeling != null){
selectedFeelingTv.setText(getString(R.string.selected_feeling) + selectedFeeling.getFeeling());
}
}
My problem is that user can also pick a photo from Gallery and when he does that my selected feeling turns out to be null. I think that happens because the activity in which I save it is restarted, so how could I persist that data?