how to sent data firebase Auth data to multiple activity of fragment in android | kotlin

Issue

I am doing an e-commerce app, do I need to pass the AUTH information like id from Login activity -> Home Fragment -> product activity -> product details (when user add to favor list it will be based on the user id, add to firebase which is sub-collection under user list)

I try to pass the data from Login activity -> HomeFragement -> product ->details ->account

can I do like this getExtra from then put extra to(it will be like a middle man to pass data between them):

var docIDAuth  requireActivity().intent.getStringExtra("docIDFromLoginAuth")
var intent  Intent(requireActivity(), ProductDetails::class.java)
intent.putExtra("docIDFromLoginAuth", docIDAuth)
startActivity(intent)

or call it to pass to multiple activities/fragment in one times

var docIDAuth  requireActivity().intent.getStringExtra("docIDFromLoginAuth")
var intent  Intent(requireActivity(), Login::class.java)
intent.putExtra("docIDFromLoginAuth", docIDAuth)
startActivity(intent)

var intent  Intent(requireActivity(), HomeFragment::class.java)
intent.putExtra("docIDFromLoginAuth", docIDAuth)
startActivity(intent)

     var intent  Intent(requireActivity(), Prodcut::class.java)
intent.putExtra("docIDFromLoginAuth", docIDAuth)
startActivity(intent)

var intent  Intent(requireActivity(), ProductDetails::class.java)
intent.putExtra("docIDFromLoginAuth", docIDAuth)
startActivity(intent)

or should I use interface or ViewModel to do it?

Can u show the sample code to tell me how to implement it. I am new to this.

Solution

I need to pass the AUTH information like id from Login activity -> Home Fragment -> product activity -> product details

There is no need to pass the UID from one activity to another. If your user is logged in, then you should simply use:

val uid  FirebaseAuth.getInstance().currentUser?.apply {
    Log.d("TAG", uid)
    //Add the product to favorites under the UID
}

In each activity/fragment is needed.

Answered By – Alex Mamo

Leave a Comment