How to send data back from Activity to Activity with Navigation Component - android

I know Navigation Component is ideally designed to be used in one-single-activity app. However, Android allows now add <activity> items within a NavGraph. My app is kind of following the one-single-act pattern, but there is a moment where FragmentA has to navigate a DialogFragment.
I would navigate to it by using:
Navigation.findNavController(v).navigate(R.id.actionToFragmentB);
but I cannot because the problem described here, so I ended up deciding to navigate to this Fragment by navigating to an ActivityB and placing the Fragment within it by using FragmentTransaction
Navigation.findNavController(v).navigate(R.id.actionToAcvitityB);
Summing up, my approach has to navigate from FragmentA within ActivityA to ActivityB, and this last will be in charge of showing the FragmentB by FragmentTransaction. Or, I have to navigate from FragmentA within ActivityA to, ¿¿somehow??, FragmentB.
My real problem is: how do I send data back from ActivityB to ActivityA??? Or from FragmentB to the FragmentA??? Taking into account that having two Activities I do not have the same NavController.

Related

MVVM Single Activity app return to a nested fragment with Navigation component after leaving app or launching intent

I am trying to return to a specific fragment after pressing the home button, sharing data with another app or switching activities.
Fragments in bottomNav:
A B C D
Fragments that are shared around the app and can be nested or navigated to from the bottomNav fragments:
E F G
Intended behaviour
User navigation stack:
A -> E
Presses home button and reopens app, stack remains the same.
A -> E
Actual Behaviour
User navigation stack:
A -> E
Presses home button and reopens app, brought back to first fragment
A
I tried to restore the navigation state by passing a bundle from navController.saveState() to a viewmodel during the Main Activity onPause method and restoring it with navController.restoreState() onResume to no avail. Is this not capable with the navigation component?
What you are looking for is called Multiple Backstack. Every Fragment in android maintains its own stack. So if you navigate to another Fragment in the ParentFragment, your ParentFragment will add the childFragment in its stack.
In your case, your ParentFragment is A and childFragment is E.
However, NavigationComponent has no vanilla support for Multiple Backstack. Ian Lake, the creator of NavigationComponent library is working on this issue since NavigationComponent was launched, and still haven't delivered on it. This doesn't mean he is not working on it, it simply means (as stated by Ian himself) that its harder to achieve because of Fragments API.
Coming back to your question, if you really want to implement MultipleBackstack then you can follow this project which pretty much anyone who wants MultipleBackstack with NavigationComponent first refers to. There is also a google sample that shows how to achieve this behavior.

How do I recreate a backward navigation for Fragments from a notification bar on 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
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.

Android: How can you pre-populate the fragment backstack?

I have an app that has one Activity that loads fragments… on initial startup it loads fragment A. From A the user can navigate to fragment B; and from B navigate to fragment C. Each time a fragment is replaced I do addToBackStack. The back button navigates as one would expect, C to B to A, and if you press back again the app exits.
I’m also using a Dropbox datastore to save all my app’s data so I can move seamlessly between devices and have all my data synced. This seems to be working well. As part of my data I store which fragment was last displayed. Now when my app starts on a second device it correctly displays the most recently opened fragment. This works fine, however, I no longer have a backstack. For example, if the app is showing fragment C device one and then I start the app on device two, as expected fragment C is shown on device two. But when I press the back button, the app exits instead of showing fragment B. (Which makes sense since on device two the app only loaded fragment C.)
So my question: How do I pre-populate my baskstack such that when pressing the back button on device two that it navigates to fragment B?
Check out TaskStackBuilder or the activity XML declaration android:parentActivityName.
I solved this by just committing multiple fragment transactions. Works for me
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment1(), "f1").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment2(), "f2").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment3(), "f3").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment4(), "f4").commit()
However, for a cleaner and better solution you could also have a look at the Jetpack Navigation UI

Handling to reopen fragment on back press

Let me explain my application first.
I have an activity and fragments in it. I'm opening fragmentA onCreate of application. In fragmentA,before i open fragmentB by using support fragment manager's replace() method, i call a webService for some json datas.
Then i open fragmentB with these json datas. After i opened the fragmentB, i want to turn back to fragmentA by using device's default backbutton, and i don't want to handle backButtonPress(). Just go back to previous fragment. My app does this.
Here is the question. When i press back button, my fragmentA calls the service again and it affects the user to wait. But how can i just turn to the previous fragment ?
I am looking for the best solution to turn previous fragment.

Understanding the fragment and activity lifecycle and backwards navigation

I'm trying to understand some odd behavior. I have an ActivityA that calls a method in onCreate() to add FragmentA to R.id.fragment_container. Inside FragmentA I have a button that attaches FragmentB by using ActivityA's fragment manager (getActivity().getSupportFragmentManager()) and replacing the R.id.fragment_container and I also add it to the backstack. I also have another button that starts a new ActivityB.
When I navigate back from ActivityB I get: ActivityA onResume(), FragmentA onResume(). But when I navigate back from FragmentB I get: FragmentB onCreateView(), FragmentB onActivityCreated() then the 2 onResume().
So my questions is...why is the view state saved when a new activity is launched and not when the fragment is replaced and reattached. It looks much better to just restore that state rather than recreate the views and fetch that data again. This seems like opposite behavior from what I would expect so I'm clearly missing some fragment state saving/restoration step or something. It seems like the activity is just pausing FragmentA (and ActivityA) when ActivityB is launched and restoring it on back pressed but when FragmentB is attached FragmentA gets completely destroyed. I'm sure there's a way to prevent this I just can't seem to figure it out. Thoughts?
Just below your question are four tags android,android-fragments,android-lifecycle& android-navigation .Put your cursor over it for a while a black box will pop up . click on info tab and you will get best links to study that topics along with links to books .
Hope this will help you

Categories

Resources