Is there a way for sending data from a fragment
to another without create new instance
from it ?
I inject
my fragment
but i faced with a problem that i describe it in below :
We inject
our fragments
like this :
val fragmentModule= module {
factory { MyFragment() }
}
And this is a function
for replace fragment
:
fun AppCompatActivity.replaceFragment(fragment: Class<out Fragment>,frameId: Int){
supportFragmentManager
.beginTransaction()
.replace(frameId,fragment,null,null)
.commit()
}
Then i use it like this :
activity.replaceFragment(MyFragment::class.java,R.id.frameLayoutId)
So my question is how can i set argument
to fragment
in this way and without create new instance
of fragment
how can we pass a data ?
val bundle=Bundle()
bundle.putString("YourKey","YourData")
val fragment=MyFragment()
fragment.arguments=bundle
without create new instance?
NO . Dependency injection framework only takes care of provide the dependencies where needed it does not connect the Component automatically in any way.With
Koin
you can pass theBundle
during creation of fragment using aFragmentFactory
.To pass data after the fragment created you have set it in
Fragment
somehow. There can be following options:-MVVM
you can use a sharedViewModel
to pass the data usingLiveData
.Fragment
from back stack and call a method on it.