Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android navArgs clear on back

Is there a way to clear navArgs after using them? I have fragment A that opens fragment B with navArgs, then I navigate to fragment C and the user presses back, so fragment B is opened with the same navArgs and I don't want that.

Is there a way to navigate back to fragment B without the navArgs?

Thanks.

like image 299
Sharas Avatar asked Jun 29 '20 13:06

Sharas


1 Answers

The answer suggested by Juanjo absolutely does work. The only caveat is that you can't use the navArgs property delegate to get at them since it's wrapped Lazy. Instead you just go through the underlying arguments bundle.

For example, in FragmentB

// don't need to pull in the navArgs anymore
// val args: FragmentBArgs by navArgs()

override fun onResume() {
  when (FragmentBArgs.fromBundle(arguments!!).myArg) {
    "Something" -> doSomething()
  }
  // clear it after using it
  arguments!!.clear()
}

// now they are cleared when I go back to this fragment

like image 102
Matthew Fisher Avatar answered Sep 20 '22 14:09

Matthew Fisher