-1

I use navArgs and also I have read that I should use old school bundle in my case. Android navArgs clear on back

I have the same issue as above:

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.

This is my Fragment A where I pass some data using args and I would like to pass one data using bundle to clear that after all.

class FragmentA : Fragment() {
    private val args by navArgs<WorkItemsListFragmentArgs>()
[...]

                    val bundle = Bundle()
                    bundle.putBoolean("data", true)
                    val fragmentB = FragmentB()
                    fragmentB.arguments = bundle
                    fragmentManager?.beginTransaction()?.replace(R.id.main,fragmentB)?.commit()

// R.id.main is my main navigation layout

navArgs default library file

@MainThread
public inline fun <reified Args : NavArgs> Fragment.navArgs(): NavArgsLazy<Args> =
    NavArgsLazy(Args::class) {
        arguments ?: throw IllegalStateException("Fragment $this has null arguments")
    }

fragmentB where I also use some data from safe navArgs

    private val args by navArgs<FragmentB>()

            val argsArguments = this.arguments
            val inputData = argsArguments?.getBoolean("data")
// using some method then clear
            argsArguments.clear()

My error regarding to this line where I would like to implement new FragmnetB Args

        at com.android.fragments.reporting.FragmentB.getArgs(FragmentB.kt:59)

59 this line

    private val args by navArgs<FragmentB>()

My suggestion is I somehow overwrite something in args and it doesn't work anymore. what is the solution for it?

EDIT1:

I have changed R.id.main to R.id.navHostFragment still the same error.

I think I understand my issue:

"Replace whatever is in the fragment_container view with this fragment, and add the transaction to the back stack"

when I use replace it clear the fragment_container so it clears all the args from navigation as I understand. I could useadd, but it doesn't work as well.. still if you you have any suggestions how to handle it feel free to communicate :) Thanks

EDIT2 regarding to comment:

fragmentA

                if(isNavigated) {
     
                    findNavController().navigate(
                        FragmentADirections.actionFragmentAToFragmentB(
                            id = item.id,
                            isNavigated = isNavigated
                        )
                    )

fragmentB

var isNavigated = args.isNavigated
            if (isNavigated) {
                add(Type.TIME)
                isNavigated = false
    // args.isNavigated = false is impossible cuz immutable value
            }

Ok my solution:

-> use sharedViewModel -> use shared pref -> use companion object (not efficient) -> change state of element in recyclerview for example i.e. toNavigate

Thanks for answers if you have better idea please share.

Takeshi567
  • 19
  • 4
  • Everything you're doing is wrong: you cannot ever use a Fragment transaction wehn you are using the Navigation Component. You shouldn't be using arguments as events in the first place. Can you explain 1) what this data actually is 2) the code that uses the data. – ianhanniballake May 20 '22 at 17:49
  • This data it is just boolean which is named: "isNavigated" = false, if you navigate through fragment it goes to true, simply if(isNavigated) { method navigate further and here I should make isNavigated = false }, but I can't with safe args. I can't clear arguments. I EDITED MAIN POST. Thank you for answer. And for trying to discuss with me to solve the problem. – Takeshi567 May 21 '22 at 07:13
  • But the only way you get to fragmentB is by navigating to it, why do you need an argument for that? This sounds like an [XY Problem](https://xyproblem.info/) where you need to step back 4 or 5 more steps back to the core problem you're trying to solve without thinking about arguments or anything. Why is fragmentA (another non-specific name that would be better if you could just be specific) doing what it is doing? – ianhanniballake May 21 '22 at 20:29
  • I do not understand what you mean. I am doing navigation then i press back and I have bug with auto navigation beacause I wrote in my code if statement depends on immutable value from safe args navigation. What more you need to know in that case to help? What kind of code you need I do not understand. I navigate fragmentA -> FragmentB -> fragmentC then I come back and my if still have got this immutable valuue form args and it works... Thanks, tell me exactly what you need and I will try to give me more piece of information. – Takeshi567 May 22 '22 at 23:16
  • Why is fragmentA 'auto navigating' to another fragment? When does it 'auto navigate'? What does 'auto navigate' mean exactly (include your code and where exactly this is called - from a lifecycle method, somewhere else, etc.)? – ianhanniballake May 23 '22 at 00:16
  • In FragmentA I have setOnClickLongPress if it is activated action goes with isNavigated = true (send this information via args safe navigation) and it navigate through fragmentB to fragmentC, then if someone press back he is in the loop, because boolean is true all the time. Thank you for answer I hope I wasn't like rude or something, just want to find the right solution I really appreciate that you communicate and have discussion with me. I was thinking I could use shared prefereneces or even companion object, but I think the best option would be just reset it after using if it is possible. – Takeshi567 May 23 '22 at 07:11
  • Basically my question is: How to send mutable variable between fragments in Android using Kotlin? In case I use safe args. I was thinking about shared preferences, but maybe it is something different. – Takeshi567 May 23 '22 at 07:48
  • Ok my solution: -> use sharedviewmodel -> use shared pref -> use companion object (not efficient) -> change state of element in recyclerview for example i.e. "toNavigate" Thanks for answers if you have better idea please share. – Takeshi567 May 23 '22 at 11:52
  • Why doesn't fragmentA just call navigate twice, first to fragmentB, then to fragmentC? Why is fragmentB involved at all? – ianhanniballake May 23 '22 at 13:47
  • Because when I press back i would like to popBack to the fragmentB – Takeshi567 May 23 '22 at 15:14
  • If fragmentA calls navigate twice, first to fragmentB, then to fragmentC, then hitting the back button would take you to fragmentB. So again, why is fragmentB involved at all? – ianhanniballake May 23 '22 at 20:35
  • I mentioned I want to back to fragmentB so I need it – Takeshi567 May 24 '22 at 06:37
  • So just to be clear, your actual root problem is that you are on fragmentA and you want it to appear to the user that they navigate directly to fragmentC, but when they hit the back button they'll be taken to fragmentB first before returning to fragmentA? – ianhanniballake May 24 '22 at 13:09
  • Yes it is like this – Takeshi567 May 25 '22 at 09:43

0 Answers0