I am planning on having 3 fragmentlists contained within one activity. The goal is you select the talk option from the first list, then it transitions to the run list based on what you clicked in talk list, and then in the run list, based on what you click it will transition to the final eat list. Should this be happening in the fragments themselves(like i have it) or calling the activity to handle passing data back and forth to the fragments?
public class OptionsActivity extends Activity {
protected TalkFragment talk;
protected RunFragment run;
protected EatFragment eat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
talk = new TalkFragment();
run = new RunFragment();
eat = new EatFragment();
}
}
public class TalkFragment extends ListFragment {
private Cursor mCursor;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
}
@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
mCurCheckPosition = pos;
// We can display everything in-place with fragments.
// Have the list highlight this item and show the data.
getListView().setItemChecked(pos, true);
// Check what fragment is shown, replace if needed.
RunFragment run_frag = (RunFragment) getFragmentManager().findFragmentById(R.id.fragment_run);
if (run_frag == null || run_frag.getShownIndex() != pos) {
run_frag = RunFragment.newInstance(pos);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
This is obviously just snippits, but you get the idea. If I do it this way, I am not really sure how to pass certain parameters to fragment properly. Ideally, the RunFragment would know what to display based on the id of the item clicked in the TalkFragment. Should these be going through the Activity instead?
The way I typically do it is have the Activity be the traffic cop for handling the fragments. Your onListItemClick implementation could just tell the Activity what it wants to do: