0

I am very new to taking an android application and for a project I need a BottomNavigati but I cannot get my data that comes from the beginning of the session to pass to the main fragment

Activity:

val bundle1 = Bundle()
    bundle1.putString("User",User)
    bundle1.putString("Correo",Correo)
    bundle1.putString("Region",Region)

    val navView: BottomNavigationView = findViewById(R.id.nav_view)
    val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    val navController: NavController = navHostFragment.navController
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
        )
    )
    navHostFragment.arguments = bundle1
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)

HOMEFRAGMEN:

    private lateinit var homeViewModel: HomeViewModel


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
    val root = inflater.inflate(R.layout.fragment_home, container, false)
    val textView: TextView = root.findViewById(R.id.textView4)
    return root
}

}

Skyllerb
  • 15
  • 2

1 Answers1

0

There is no direct access to HomeFragment from MainActivity in your case. BottomNavigation uses nested navigation architecture pattern with nested graphs. More details here: Passing argument(s) to a nested Navigation architecture component graph

But you can use alternative way to pass data from Activity to Fragment using context.

Step 1 - Describe HomeFragmentData model:

data class HomeFragmentData(
  val value: String
)

Step 2 - Describe HomeFragment interface:

interface IHomeFragment {
  fun getHomeFragmentData(): HomeFragmentData
}

Step 3 - Implement interface to your Activity:

class MainActivity : AppCompatActivity(), IHomeFragment {

  override fun getHomeFragmentData(): HomeFragmentData {
    return HomeFragmentData(
      value = "Home Fragment data"
    )
  }

  // Rest of your code
}

Step 4 - Call function getHomeFragmentData() from HomeFragment using context:

class HomeFragment : Fragment() {
    private var interactionListener: IHomeFragment? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)

        when(context) {
            is IHomeFragment -> {
                interactionListener = context
            }
            else -> throw RuntimeException("$context has to implement IHomeFragment")
        }
    }

  override fun onCreateView(
      inflater: LayoutInflater,
      container: ViewGroup?,
      savedInstanceState: Bundle?
  ): View? {

    val root = inflater.inflate(R.layout.fragment_home, container, false)
    val textView: TextView = root.findViewById(R.id.text_home)
      interactionListener?.getHomeFragmentData().let {
          textView.text = it?.value
      }

    return root
  }
}