0

I have two fragments A and B. I'm trying to navigate to B and then back to A. While doing this, I have some form data (a bunch of text fields) in A which I'd like to preserve while navigating back and forth to B. When I navigateUp() from B, the data in A is preserved but I'm not able to pass data from B to A. If I use bundles while navigating back to A, I'm unable to save the data using onSaveInstanceState(). I've used the following code in fragment A for saving the form data (got it from: https://developer.android.com/guide/components/activities/activity-lifecycle#instance-state)

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if(savedInstanceState!=null){
            with(savedInstanceState) {
                inputName.setText(getString("Name"))
            }
        }
        else{
            //I just leave it since the text fields will be empty anyways
        }
    }

override fun onSaveInstanceState(outState: Bundle) {

        outState.run {
            putString("Name",inputName.text.toString())
        }
        super.onSaveInstanceState(outState)
    }

But onSaveInstaceState is not being called when I navigate away (due to this when onCreate is called, the value ends up as null and the flow goes to the if statement). All I want to do is to save data in A while navigating from A to B.

Anirudh Ganesh
  • 446
  • 4
  • 22

1 Answers1

1

Not the best approach to follow (I'd suggest to share data using shared stateful view model but from your question it is not clear if you are using view models at all) :

  1. Supply your fragment B with action like this:
        <action
            android:id="@+id/backToA"
            app:destination="@+id/A"
            app:popUpTo="@+id/B"
            app:popUpToInclusive="true" />
  1. Supply your fragment A with needed arguments that you'll pass from B.
  2. From B, instead of navigateUp use navigate with action backToA and supply arguments you need to provide to A

You'll also have to take care of manual handling of "back by system back button" scenario (to override navigateUp), as well as, possibly, "back by toolbar back" if you have toolbar - but that's another story.

ror
  • 3,295
  • 1
  • 19
  • 27
  • As of now I am navigating like how you've mentioned and I am passing arguments. But when I override back button and handle the toolbar up button, it kinda creates a messy backstack (so if the user keeps pressing back all the instances of A and B keep popping back one by one). I think I should pop out the fragment from the back stack to prevent this.Also, I am using a ViewModel but not a shared one though (since both fragments have DB operations didn't want to load up everything in one view model). – Anirudh Ganesh Jun 02 '20 at 13:42
  • 1
    You can set pop property while creating your action using the navigation component. So when you go to B from A, you pop A from the back stack. When you go from B to A, as mentioned in the answer, you pop B. You can make the code a bit clean this way. – Devansh Maurya Jun 02 '20 at 17:29
  • 1
    You can create a new view model for sharing data between fragments. You can have multiple view models for the same fragment, so that should probably reduce the amount of code in one view model. – Devansh Maurya Jun 02 '20 at 17:31