ruffy
ruffy

Reputation: 391

Fragment transactions: how to exchange data to former fragment

Okay, so I have a MainActivity.java which has a Fragment, called FavoritesFragment.java, at some point I call a MyListFragment.java where people are supposed to choose a list element. After that I want to get their choice back in my FavoritesFragment.java.

So I implemented an interface for that. In MyListFragment, a method is called, that sets settings in FavoritesFragment:

From MyListFragment:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Station s = (Station) l.getItemAtPosition(position);
    updater.stationChosen(s, isFrom);
    getFragmentManager().popBackStack();
}

Where updater is my interface.

So, everything works fine, my Content in FavoritesFragment is updated and everything. But after I call popBackStack() everything in FavoritesFragmentreset to former glory.

When I used to do everything with Activities there was this onActivityResult(...) callback from which you could exchange data. How can I do this with fragments? I don't want to use Activities for this minor case, and I want to have the list in it's own class, because I'm going to need it in other classes.

Upvotes: 0

Views: 43

Answers (2)

Jschools
Jschools

Reputation: 2758

You need some place to keep track of favorites that is outside of the Fragments. One place is in the Activity that hosts the Fragments, which you can access via getActivity() and casting it to MainActivity or some interface that you define.

For example:

public interface FavoriteManager {
  void onFavoriteAdded(Station s);
  void onFavoriteRemoved(Station s);
  List<Station> getFavorites();
  etc.
}

public class MainActivity extends FragmentActivity implements FavoriteManager {
  ...
  private List<Station> mFavorites;
  ...
  @Override
  public void onFavoriteAdded(Station s) {
    mFavorites.add(s);
  }
  ...
  @Override
  public List<Station> getFavorites() {
    return mFavorites;
  }
}

public class FavoritesFragment extends Fragment {
  ...
  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Station s = (Station) l.getItemAtPosition(position);

    FavoriteManager manager = (FavoriteManager) getActivity();
    manager.onFavoriteAdded(s);

    getFragmentManager().popBackStack();
  }
  ...
}

public class MyListFragment extends Fragment {
  ...
  @Override
  public void onStart() {
    List<Station> favorites = ((FavoriteManager) getActivity()).getFavorites();
    populateUi(favorites);
  }
  ...
}

Another option is to use a database and a ContentProvider, as shown here: http://developer.android.com/guide/topics/providers/content-providers.html

Upvotes: 1

Bert Hartmann
Bert Hartmann

Reputation: 29

Perhaps it's better to think about the fragments existing simultaneously than one after the other (like activities would). You aren't calling a Fragment and getting a result, but calling the fragment and allowing it to change data.

I would think the easiest thing to use the Observer pattern to have your FavoritesFragment observe changes on the underlying data, and make sure that the update from MyListFragment calls notifyobservers(). Then when FavoritesFragment comes back to the front, it'll notice the change

Upvotes: 0

Related Questions