Question

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();

enter image description here

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.

No correct solution

OTHER TIPS

To control what Fragment you go back to you just need to control the entries in the backstack. FragmentManager will let let you pop the backstack so you can reset what back does once you navigate to the details fragment. There are a number of posts about this on SO - here's one and here's amonther that provides some more detail.

The one problem with this is that by overriding the natural flow of the back stack you run the risk of creating navigation that does not stem logically from the app. But you know your structure better than the system. If it makes sense to always go from detail to master, go for it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top