0

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?

Rob
  • 2,243
  • 4
  • 29
  • 40

3 Answers3

1

You can use this in your activity in order to keep the value:

@Override
  protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putSerializable("selectedFeeling", selectedFeeling);
  }

And to retrieve the value, you use this (also in the activity):

@Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if(savedInstanceState != null){
            if(savedInstanceState.getSerializable("selectedFeeling")!=null){
            selectedFeeling = (Feeling)savedInstanceState.getSerializable("selectedFeeling");
        }
    }
}

What is happening in the code above: you save the instance when the activity is destroyed and then retrieving it when the activity is restored. It's important to highlight that you need to re set the value so your fragment can read it on its onResume method.

0

Use onSaveInstanceState(Bundle outState) of your Activity to save the feeling in outState. And then read the feeling from savedInstanceState in onCreate(Bundle savedInstanceState). Do the same with all your state variables. Then you don't worry when and for what reason Android destroys your activity. It will "resurrect" again with the previous state.

WindRider
  • 11,958
  • 6
  • 50
  • 57
0

The Android way of exchanging data between fragments and activities is by using broadcasts and broadcast receivers. Therefore , I would suggest that you do the following: In your fragment , create your broadcast

public void createBroadcast()
{
    Intent broadcast = new Intent();
    broadcast.setAction(RequestCode.SUBMIT_BROADCAST);//This code is a static int just to identify your broadcast
    broadcast.putExtra("String identifier", your data);
    context.sendBroadcast(broadcast);

}

and fire it when user selects something (your feeling)

In the activity , register your broadcast receiver and update the activity

 private BroadcastReceiver br;
 ......
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle =intent.getExtras();
            String key =bundle.getString("your string identifier"); //the code for updating your activity goes next

        }
    };

IntentFilter filter = new IntentFilter(RequestCode.SUBMIT_BROADCAST);
this.registerReceiver(br,filter);
}
BMU
  • 429
  • 2
  • 9