Home > OS >  How to access a function ,written in a fragment, in an activity
How to access a function ,written in a fragment, in an activity

Time:07-26

I have created a function in a fragment that gets data from the fragment's UI. I would want to access it in my main Activity so as to store the data in cloud firestore

This is the function in the fragment override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState)

    fun getData(){
        var dairyMealStock: Int = view?.findViewById<EditText>(R.id.et_dairy_meal_stock)?.text.toString().toInt()
        var dairyMealDuration: Int = view?.findViewById<EditText>(R.id.et_dairy_meal_used)?.text.toString().toInt()
        var maizeCharmStock: Int = view?.findViewById<EditText>(R.id.et_maize_charm_stock)?.text.toString().toInt()
        var maizeCharmDuration: Int = view?.findViewById<EditText>(R.id.et_maize_charm_used)?.text.toString().toInt()
        var wheatBrandStock: Int = view?.findViewById<EditText>(R.id.et_wheat_brand_stock)?.text.toString().toInt()
        var wheatBrandDuration: Int = view?.findViewById<EditText>(R.id.et_wheat_brand_used)?.text.toString().toInt()
        var maizeBrandStock: Int = view?.findViewById<EditText>(R.id.et_maize_brand_stock)?.text.toString().toInt()
        var maizeBrandDuration: Int = view?.findViewById<EditText>(R.id.et_maize_brand_used)?.text.toString().toInt()
        var cottonCakeStock: Int = view?.findViewById<EditText>(R.id.et_cotton_cake_stock)?.text.toString().toInt()
        var cottonCakeDuration : Int = view?.findViewById<EditText>(R.id.et_cotton_cake_used)?.text.toString().toInt()
        var pyrethrumStock: Int = view?.findViewById<EditText>(R.id.et_pyrethrum_stock)?.text.toString().toInt()
        var pyrethrumDuration: Int = view?.findViewById<EditText>(R.id.et_pyrethrum_used)?.text.toString().toInt()
        var dcpStock: Int = view?.findViewById<EditText>(R.id.et_D_C_P_stock)?.text.toString().toInt()
        var dcpDuration: Int = view?.findViewById<EditText>(R.id.et_D_C_P_duration)?.text.toString().toInt()
        var blockStock: Int = view?.findViewById<EditText>(R.id.et_block_stock)?.text.toString().toInt()
        var blockDuration: Int = view?.findViewById<EditText>(R.id.et_Block_duration)?.text.toString().toInt()
        var jotoStock: Int = view?.findViewById<EditText>(R.id.et_joto_stock)?.text.toString().toInt()
        var jotoDuration: Int = view?.findViewById<EditText>(R.id.et_joto_duration)?.text.toString().toInt()
        var yaMaziwaStock: Int =view?.findViewById<EditText>(R.id.et_ya_maziwa_stock)?.text.toString().toInt()
        var yaMaziwaDuration: Int = view?.findViewById<EditText>(R.id.et_ya_maziwa_duration)?.text.toString().toInt()
        var allPurposeStock: Int = view?.findViewById<EditText>(R.id.et_all_purpose_stock)?.text.toString().toInt()
        var allPurposeDuration: Int = view?.findViewById<EditText>(R.id.et_all_purpose_duration)?.text.toString().toInt()
    }
}

CodePudding user response:

You have to use an interface to do that ..

  1. first define an interface
interface PassingDataCallback{
fun passData(str:String)
}

2 . Override onAttach in your fragment and then define your callback function

for eg

callback = when {
activity is MainActivity -> {
activity as MainActivity
}
context is MainActivity -> {
context
}

else -> {
throw RuntimeException ("Activity must implement callback");
}
}
  1. implement this callback in your activity and override the methods. Use what you need with that data

CodePudding user response:

You can use an activityViewModel and store the data there. That way both activity and fragment will have access to the same data.

  • Related