androidandroid-fragmentsandroid-activityup-button

Navigate up to last shown fragment


I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).

When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.

Any ideas how to solve that problem?


Solution

  • Make method in MainActivity for example setFragment(int whichFragment); and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.

    For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.

    Best regards