In the MainActivity I have the NavigationDrawer which looks like this:
private void displayView(int position) {
ListFragment listFragment = null;
switch (position) {
case 0:
listFragment = new AlbumFragment();
break;
case 1:
listFragment = new TrackFragment();
break;
default:
break;
}
if (listFragment != null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frame_container, listFragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
Within my AlbumFragment I can switch to the TrackFragment by clicking on listitems.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// get album & artist of selected album
String album = ((TextView) v.findViewById(R.id.albumTitle)).getText().toString();
String artist = ((TextView) v.findViewById(R.id.albumArtist)).getText().toString();
String[] albumFilter = {"albumFilter", album, artist};
// Create new fragment and transaction
Fragment newFragment = new TrackFragment();
Bundle args = new Bundle();
args.putStringArray("filter", albumFilter);
newFragment.setArguments(args);
FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.frame_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
Now the following problem occurs: If I press the Backbutton everything is fine and I'm back in the AlbumFragment. But If I open the NavigationDrawer and select the Tracksfragment and then press the Backbutton I get back to the AlbumFragment and behind it is still my TrackFragment.
Further Explanation: When I select an Album in the AlbumFragment only the Tracks from this album are shown. If I select Tracks from the NavDrawer ALL tracks are displayed.
So basically all I want is, that the entire Backstack-History is cleaned as soon as I select an Item from the NavDrawer. I already tried most of the solutions to similar problems found on here but unfortuneatley nothing worked for me so far.
Has anybody got a solution for this problem?