I'm attempting to implement a pretty basic navigation structure using Actionbar tabs and fragments. Basically I have three fragments which are selected using the tabs on an actionbar. Each one of these fragments is a listview which should allow the user to select an item and open a detail view.
I'm wondering the best way to add a new fragment to the screen from an existing fragment. I currently have the detail views all implemented as activities (which obviously is not ideal since the action bar tabs aren't there, and navigation using the back button returns to tab 1 regardless of current location).
I'm adding all of the list fragments in my MainActivity;
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (tab.getPosition() == 0) {
Frag1 frag = new Frag1();
ft.replace(android.R.id.content, frag);
} else if (tab.getPosition() == 1) {
Frag2 frag = new Frag2();
ft.replace(android.R.id.content, frag);
} else if (tab.getPosition() == 2) {
Frag3 frag = new Frag3();
ft.replace(android.R.id.content, frag);
}
}
What do I need to implement on those fragments to launch a detail view? I've attempted something like this, but navigation still does not work how I would like it.
private void onListItemClick(View v, int pos, long id) {
Log.d(TAG, "Clicked at position: " + pos);
NewsModel selectedModel = newsItems.get(pos);
Log.d(TAG, "Item: " + selectedModel.getTitle());
NewsDetailFragment fragment = NewsDetailFragment.newInstance(selectedModel);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, fragment);
ft.addToBackStack(null);
ft.commit();
I'm coming generally from the iOS world so some of my paradigms might be a little out of wack with how Android is meant to function.
Thanks!