Search code examples
androidandroid-activityfragmentandroid-lifecycle

Passing data to Fragment before destroying Activity


I've

Activity A -> Fragment A

[from Fragment A I decide to create Activity B]

Fragment A -> Activity B.

When I press back button, I go back to Fragment A which contains data list. I'd like to update this list when back button is pressed. No, I don't store data locally and making another request is too expensive. Is there a simple way to pass modified object X on back button press [From Activity B to Fragment A]? How?


Solution

  • In fragment A you can call a method from Activity A to start an activity with result to Activity B.

    When you go back you can send that data with it. In activity A you can have the method onActivityResult to catch this result and refresh Fragment A.

    Android documentation