Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I recreate a backward navigation for Fragments from a notification bar on Android?

Tags:

android

The android docs said that I can use the class TaskStackBuilder to recreate a back navigation behavior when an inner activity is launched from another way that the normal flow (from the main home activity), for example when the user click a notification bar:

"Ordinarily, the system incrementally builds the back stack as the user navigates from one activity to the next. However, when the user enters your app with a deep link that starts the activity in its own task, it's necessary for you to synthesize a new back stack because the activity is running in a new task without any back stack at all." - developers.android.com

That is ok, but I have an application most compose of fragments and few activities. I wanted to launch a inner fragment from a notification (call it FragmentC), so when the user click the notification in the notification bar my application open the baseActivity, and this activity should recreate the FragmentC as the first showing (I think to do this by passing a some args from the notification Intent and ask it in the baseActivity to replace the fragment showing), but in this scenary, when the user navigate to backward the application finish because there is only one activity alive and then the home screen is show to the user, that is wrong, I wanted to recreate a flow of fragments in this manner:

Notification clicked by the user ::

   BaseActivity -> FragmentA -> FragmentB -> FragmentC (Visible)

Then the user navigate back ::

   BaseActivity -> FragmentA -> FragmentB (Visible)

User navigate back ::

   BaseActivity -> FragmentA (Visible)

User navigate back ::

   (Application Finish, showing home screen)

Is there a way to launch a inner fragment from a notification (in the notification bar), and recreate a "normal" back navigation flow for fragments like using the TaskStackBuilder class? or How would you do it in a clean way?

I suppose that I can do this manually in the baseActivity when I detect that the fragment is launched from a notification, but this is dirty code for me, and I know that there is a TaskStackBuilder class for do this in a more clean way, but I can't found nothing about how using it with fragments transactions. :S

Sources:

https://developer.android.com/training/implementing-navigation/temporal.html#SynthesizeBackStack

https://developer.android.com/training/implementing-navigation/temporal.html#back-fragments

like image 276
Viktor Valencia Avatar asked Jan 18 '26 04:01

Viktor Valencia


1 Answers

I've created a simple class that handles the navigation, but essentially, everything what you need it's create a transaction from the FragmentManager adding what normally you need, and then, use the addToBackStack to guarante that your fragment remains in the stack

FragmentTransaction ft = FragmentManager.beginTransaction();
ft.addToBackStack(""); //An optional name for this back stack state, or null.
ft.commit();

Now when you press the back button, automatically the previous fragment comes to foreground

You can clean your stack if you want using.

fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

I think that this is the easiest way, hope that somebody else can complement or suggest something better.

like image 95
David Alejandro Burgos Rodas Avatar answered Jan 19 '26 19:01

David Alejandro Burgos Rodas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!