0

Having issues working with a few fragments.

Writing all this kotlin.

I have a MainActivity (Single Activity App). I'm trying to get the parent fragment to communicate a string to the child fragments. When following this tutorial it just loops continuously. Any other solution I've come across just results in a null pointer exception.

Basic communication between two fragments

I'm using android components navigation also, btw.

In addition I have to then while using that string send list of objects back to the parent fragment from the child fragments.

Any suggestions?

EDIT:

clarification, I am running a fragment in bottom nav enviornment that is the parent frag. Then, inside that parent frag is a view pager with a tablayout to switch between fragments. Within those fragments I am trying to add data to a list using a fab button and an alert dialog. Everything works in the individual fragments it's the data sharing is what I'm struggling with. I have not tried the bundle yet as I wasn't sure it would work. I'll give it a try though.

wesley franks
  • 6,765
  • 4
  • 16
  • 29

1 Answers1

0

Have you tried using bundle?

var bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)

you can get the data in the destination fragment like so

arguments?.getString("amount")

this is for sending data from parent to child, as for sending it backward I'm not sure if you can actually do that, as far as I know when you press back you just destroy the current fragment and bring up the previous fragment to screen, you aren't creating or calling it again so I don't think you can send any data without actually recreating the parent fragment.

MoTahir
  • 863
  • 7
  • 22
  • updated my question for clarification. I haven't tried the bundle will give that a go. The concept was a bit confusing. – wesley franks Dec 13 '19 at 22:51
  • for most situations, bundles are used to share data between two fragments or activities too, did you give it a try? did it work? – MoTahir Dec 15 '19 at 10:26
  • The problem is I'm navigating to a new fragment. It's a viewpager (two fragments) that is needs to update the list in the parent fragment that the viewpager is inside. – wesley franks Dec 16 '19 at 02:36