I've looked around but found no real solution to my particular issue. So I have a navigation main activity, which consists of a navigation drawer and a frame layout for my content.
In my nav drawer are three buttons, each of which fill the frame layout with a specific fragment. One of those fragments acts as a "master view", and clicking on an item in that fragment then opens another fragment which is the "details view".
The problem I am having is this: If I am in the details view fragment, and I click on another button in my nav drawer to go to another fragment, then click on the nav drawer button to get back to my details view then it's fine, I get there ok. When I hit the back button however it takes me back to the previous fragment rather than the master view fragment. Is there any way I can get around this?
Here is a snippet of how I am moving between different fragments in my nav drawer:
private void selectItem(int position)
{
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = null;
Bundle args = new Bundle();
switch (position)
{
case 0:
fragment = new ReportIt();
fragment.setArguments(args);
break;
case 1:
Fragment f = fragmentManager.findFragmentByTag("article_view");
if (f == null)
{
fragment = new ReadIt();
fragment.setArguments(args);
}
else
{
fragment = f;
}
break;
case 2:
fragment = new FindIt();
fragment.setArguments(args);
break;
default:
fragment = new ReportIt();
fragment.setArguments(args);
break;
}
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).addToBackStack(null)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(mNavTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
And my master view fragment passing control to the details fragment:
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new ReadItDetail();
Bundle args = new Bundle();
args.putString("ArticleId", pr.GetId());
fragment.setArguments(args);
fragmentManager.beginTransaction().addToBackStack(null)
.replace(R.id.content_frame, fragment, "article_view")
.commit();
So as you can see from the graph, All 3 of the navigation drawer buttons are part of my main activity. Also in my main activity is a framelayout which uses the fragments shown. All boxes outside of the navigation drawer button box are fragments. The master and detail are what I am having issues with. When I move around to other fragments, and then come back to my details fragment, hit the back button, I want it to go back to the master fragment 100% of the time rather than moving back to whatever other fragment I may have been on previously.