0

first sorry for my rusty English !

This is a doubt I have had for some time now and although my skills on Android have been getting better I haven't found an elegant solution for this issue:

Lets say an application uses the Navigation Drawer pattern and has three levels of navigation: Home -> Posts -> Post details

When I open the App from the launcher I add the Home Fragment. From here the user can navigate all the way down to a given "post" using fragment transactions.

Now imagine there is some kind of notification that is letting me know something happened on a specific post: How would you create a "synthetic" back-stack when opening the app from the notification ? What I mean is:

1- Open the app from the notification

2- The app opens on Post Details

3- Back navigation has to be > Posts > Home

Thanks a lot in advance !

aarcoraci
  • 201
  • 3
  • 9

1 Answers1

1

without any line of code its hard to say how you are setting these Fragments , but you may need addToBackStack method or consider @Override onBackPressed properly. assuming notification is yours you can set id or whatever you need inside PendingIntent extras Bundle, obtain inside OnCreate method and then create a stack

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • yes, that is how I have solved it in the past. But I don't think it's elegant. The reason why I didn't put any code is because is more a conceptual problem I'm facing. – aarcoraci Mar 27 '17 at 19:13
  • Look at: https://medium.com/google-developers/tasks-and-the-back-stack-dbb7c3b0f6d4 There is a section that explains how to use ** TaskStackBuilder**. I would like to know if there is something similar for fragments. – aarcoraci Mar 27 '17 at 19:18
  • yes, its `FragmentManager`, and the stack is set using `FragmentTransaction`. and these methods are described in `The Back Stack and Fragments` section... – snachmsm Mar 27 '17 at 19:26
  • So, it's not a terrible practice to add more than one transaction to the backstack before a commit ? Thanks ! – aarcoraci Mar 27 '17 at 20:49