Home > database >  At what point is the data from Bundle lost?
At what point is the data from Bundle lost?

Time:01-10

I pass the data through the Bundle to the `vm, I look at everything clearly in the log, but when I try to turn arguments in the fragment where I should have received it, null comes. I can't enter at what point I could have lost them.

Here is my first vm:

fun sendData(it: ConfigurationDto?) {
    Log.d("some", "sendData : $it")
    if (it != null) {
        OnboardingViewModel.newBundle(it)
    }
}

Here is code of my OnboardingViewModel:

companion object {
    val Bundle.configuration: ConfigurationDto?
        get() = getSerializable("configuration") as? ConfigurationDto

    fun newBundle(configuration: ConfigurationDto): Bundle {
        Log.d("some", "new Bundle: configuration $configuration")
        return Bundle().apply {
            putSerializable("configuration", configuration)
        }
    }
}

Here I see that the configuration is coming.

And when i trying to get my arguments in fragment:

class OnboardingFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnClickListener {
  
      private fun setupViewModel() {
        viewModel = ViewModelProvider(this, viewModelFactory).get(OnboardingViewModel::class.java)
        lifecycle.addObserver(viewModel)

        Log.d("some", "arguments in vm: $arguments")
        viewModel.setInitialData(arguments)

I see that arguments is null.

CodePudding user response:

I see that arguments is null.

arguments is the Bundle that is set on a Fragment, usually when you create it. Something like:

val fragment = OnboardingFrament().apply {
    arguments = Bundle().apply {
        // Set values on bundle
    }
}

Since you have only shown two random pieces of code related to some random ViewModels and not where you create OnBoardingFragment, I can only assume that wherever you create it, you are not setting the arguments, hence it's null.

CodePudding user response:

Your sendData function does nothing. It creates a Bundle object and then immediately releases it to the garbage collector because you aren't doing anything with it. Incidentally, it's weird that it takes a nullable parameter when it can't do anything useful with null. If a function is useless when its parameter(s) is null, it should not accept a nullable parameter. Otherwise, the calling code looks like it's doing something useful when it isn't, which just makes debugging more difficult and bugs more likely.

Also, it is highly unusual that your ViewModels are aware of your Fragments. It is part of the contract of ViewModel that you're not supposed to leak your Activities or Fragments to them. LiveData is special because it can automatically drop its references to its observers at the appropriate time to prevent a leak.

So whatever you're doing by having your ViewModel observe your Fragment lifecycle is probably a design error. You should not be trying to send data to another Fragment by using a ViewModel function.

If you want to send data from one Fragment to another Fragment, then you should set its arguments property right after instantiating the Fragment instance and before making the fragment transaction. Then, as the framework automatically recreates the Fragment as needed for configuration changes, when it automatically creates new instances of that Fragment, it will also pass it that same argument data.

  •  Tags:  
  • Related