Issue
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.
Solution
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.
Answered By - stkent
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.