Home > Enterprise >  Android Studio: Passing argument from fragment to fragment and using it
Android Studio: Passing argument from fragment to fragment and using it

Time:02-19

I am following the official enter image description here

Because the header text is actually inside a string resource file. (strings.xml). The tutorial does not elaborate further, and assumes it should be working, but here we can see it just displays "%d" instead of the argument. Relevent line in strings.xml:

Here is a random number between 0 and %d.

How exactly do I display the passed argument wtih SafeArg, seeing how my header is displaying a string resource? The tutorial does not say anything about needing to modify anything in the resource file or second fragment class, so I am lost.

CodePudding user response:

val count = args.myArg this line is where you get the argument from (basically android is doing everything for you - meaning retrieving the argument and passing it to the fragment where you get it, ensuring it's type...)

Now this is the line where I think there might be some misunderstanding:

        val countText = getString(R.string.random_heading, count)

Here what we are doing is getting the string from strings.xml file which is : Here is a random number between 0 and %d. but what the getString method is also doing is that it is formatting the string (doing like String.format method in java) so it taking the argument that follows (here count) and replacing it in the string.

Now your countText value is Here is a random number between 0 and 15. if count = 15, ie - the number you passed down from the previous fragment.

Then in the tutorial it sets the value of the textview like this :

view.findViewById<TextView>(R.id.textview_header).text = countText
  • Related