2

I'm developing a fairly complex Android application (used for a Home Automation system) which is basically a One Activity - Multiple Fragments Application.

The Navigation in the application primarily consists of a Navigation Drawer. It has 6 Different Categories which we can name A..F. These categories can have their own Navigation Hierarchies, but I have chosen to not use an explicit Back Button in when digging down the a hierachy - Rather, I show the Hamburger Icon always. This lets the user access the Navigation Drawer at all times and relies on the user using the Hardware Back button for navigating back to the previous Fragment.

Now, I guess some of you might say this is bad practice (and I would listen if you told me how to do it instead) but I still have a problem I would like to solve without changing the Architecture, presented in the following use case:

Use case:

  • The User opens the Activity and arrives to Fragment A.
  • The User uses Drawer to go to B
  • The User navigates from B to Bb to Bc (Sub views of B)
  • The User uses Drawer to go to C
  • The User uses Drawer to go to B
  • The User navigates from B to Bb to Bc (The same sub views as before)
  • The User goes through the entire Back Stack using the Back Button

What happens is of couse that the user sees [Bc, Bb, B, C, Bc, Bb, B, A]

Now I generally think that the behaviour is correct, but will it result in a bad user experiecnce to see Views more than one time when using the Back button? If so, how can the problem be solved?

2 Answers 2

2

I am in a similar situation as you, and I completely see your point.

In my case when I switch section with the navigation drawer, a new fragment is presented. When the user navigates in the section the fragment is replaced, so the back stack grows. In order to avoid the kind of issues you are encountering, when the user goes to another section I save the state of all the fragments in the back stack, and then clear the back stack (otherwise the user will be able to go back anyway), and then eventually restore it when he goes back to the same section.

I have to admit that I think my code is not the nicest, but for the moment it's the only solution I have found to this issue. To be sincere, I am kinda surprised that there are not "out of the box" solutions to this kind of back stack switch with the navigation drawer.

6
  • This is the solution I went with in the end as well and it works as expected.
    – Joakim
    Sep 9, 2016 at 7:57
  • @Joakim Just for curiosity, how did you implement it, code wise? Just in general, to be sure I am not missing something that could simplify my implementation
    – papafe
    Sep 9, 2016 at 8:05
  • I just wiped the backstack when the user navigated (changed Fragment ) from the Navigation Drawer (getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);). When navigating from within the shown Fragment I didn't pop.
    – Joakim
    Sep 9, 2016 at 10:01
  • And I didn't add do .addToBackstack(null) when replacing the Fragments from the drawer
    – Joakim
    Sep 9, 2016 at 10:02
  • This is what I am doing too to clear the back stack. Are you also keeping a separate back stack for the different drawers elements?
    – papafe
    Sep 9, 2016 at 10:58
0

One problem with this architecture is the fragment backstack size. The way you described this the stack could get very large. Why not do this... When the user selects an item in the drawer all fragments of that type is removed from the fragment manager? In your example this would happen when tapping the back button - Bc,Bb,B,C,A

4
  • How to edit the FragmentTransaction backstack to accomplish this?
    – kaay
    Jul 8, 2016 at 9:54
  • @kaay You would just need to do a fragment replace instead of a fragment add.
    – papafe
    Sep 7, 2016 at 13:08
  • @markusian I thought the backstack held Transactions, not Fragments, and a "replace" was reversed exactly the same as an "add"?
    – kaay
    Sep 8, 2016 at 16:38
  • @kaay Sorry, I think I misunderstood, I wrote an answer to be more clear :)
    – papafe
    Sep 9, 2016 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.