2

What is the recommended way to handle back button presses in an app that contains many Fragments accessible via a navigation drawer? I see two possibilities:

  • add all Fragments to the back stack, so that when the user presses the back button he(she) returns to the previous Fragment;
  • no matter how many fragments users pass through, always exit the app when the back button is pressed.

I have always previously used the first method, but recently I received some feedback asking me to switch to the second method.

stkent
  • 19,772
  • 14
  • 85
  • 111
despecher
  • 806
  • 6
  • 15
  • Do what the requirement is! – Rohit5k2 Jan 11 '15 at 00:54
  • Disagree with the hold; as per my answer, the documentation explicitly describes the best practices. There's no ambiguity in what the best practices **are**; only in how closely a developer chooses to respect them. – stkent Jan 11 '15 at 01:19

1 Answers1

1

The Android documentation has some pretty comprehensive guidelines governing navigation patterns for the back and up buttons (with examples!). These guidelines obviously do not discuss navigation implementation (in terms of Activity vs Fragment usage) - in actual fact, it would seem that the appropriate approach may be to decide on a navigational structure first, and from there decide whether Activity-based or Fragment-based implementation suits best.

Either way, the Android docs above should tell you how to handle the issue in the most platform-sympathetic manner. Whether or not you build up a back stack (and what you do and do not place in the back stack) should be governed by the logical structure of your app's UI.

Since you seem to be using a navigation drawer, anything directly accessible from the drawer would be considered 'top level', in which case, pressing either back or up on one of those screens should probably exit the app.

stkent
  • 19,772
  • 14
  • 85
  • 111