Home > Enterprise >  Transferring values between Fragments
Transferring values between Fragments

Time:01-26

MainActivity

-A Fragment

-B Fragment

-C Fragment (has 3 sub-fragments)

-a Fragment

-b Fragment

-c Fragment

That's how my project is organized.

RecyclerView exists in the a,b,c Fragment, and the number of items generated in the RecyclerView is indicated in the textView of the a,b,c fragments.

To see the number of RecyclerView items in a,b,c Fragment at a glance, I would like to deliver the integer value to A Fragment.

    adapter = new TestAdapter();
    recyclerView_kor.setAdapter(adapter);

    ArrayList<TestInfo> result = callback.selectAll(); 
    //callback.SelectAll() is a lookup of database cumulative values.
    adapter.setItems(result);
    String countMath = result.size() "times";

    textView_kor.setText(countMath);
    AFragment Afragment = new AFragment();

    Bundle bundle = new Bundle();
    bundle.putString("textView_Math", countMath);
    Afragment.setArguments(bundle);

I tried to receive the value by putting the key value and the string to be delivered to it and entering the key value in A Fragment like

    Math_practicenumber_textVIew = rootView.findViewById(R.id.Math_practicenumber_textVIew);
    Math_practicenumber_textVIew.setText(getArguments().getString("textView_Math"));

but it didn't work... How do I fix the chords?

CodePudding user response:

Suppose this is your Fragment B

 val bundle = Bundle()
            bundle.putString("textView_Math", countMath)
            findNavController(R.id.nav_host_fragment).navigate(
                R.id.fragmentA,
                bundle
            )

and in your FragmentA

private var textName: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
        textName = it.getString("textView_Math")
    }
}

and last

 Math_practicenumber_textVIew.text=textName

CodePudding user response:

Use ViewModel to communicate between fragments but please be careful view models needs to be sharedViewModel.

  •  Tags:  
  • Related