New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot inject to fragment with ScopeActivity #1356
Comments
This started happening to me as well after updating from To workaround it I used a similar approach as above:
|
@arnaudgiuliani i think this could be solved by simply moving the This actually makes Koin 3.2.0 unusable for us as as it prevents using activity(Retained)Scoped components in Fragments without workarounds! |
We're facing the same issue which is really blocking at the moment. The solution @Pfoerd has proposed sounds good to me. |
@Pfoerd ok, interesting 👌 |
also link to #1328 |
After rethinking about the solution and checking with your feedback, I have a proposal for 3.2.1: a simpler API more than lazy delegates (harder to use and follow for lifecycle). The only breaking result in interface AndroidScopeComponent {
var scope: Scope?
fun requireScope() : Scope = scope ?: error("Trying to access Android Scope on '$this' but scope is not created")
} The idea is to have clear and simple API to use: create a scope and bind it when we need it. For Scope Android classes, the implementation will become: abstract class ScopeActivity(
@LayoutRes contentLayoutId: Int = 0,
) : AppCompatActivity(contentLayoutId), AndroidScopeComponent {
override var scope: Scope? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
createActivityScope()
}
} abstract class RetainedScopeActivity(
@LayoutRes contentLayoutId: Int = 0,
) : AppCompatActivity(contentLayoutId), AndroidScopeComponent {
override var scope: Scope? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
createActivityRetainedScope()
}
} abstract class ScopeFragment(
@LayoutRes contentLayoutId: Int = 0,
) : Fragment(contentLayoutId) , AndroidScopeComponent {
override var scope: Scope? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
createFragmentScope()
}
} The idea is to provide the following API:
One good thing is that Injection API (ViewModel ...) now take care to resolve the current scope in a lazy way. Lazy delegates API are deprecated. Please now use those new API. |
I'm trying to inject some dependency to both activity and fragment using Koin and I expect it to live as long as activity lives, but it turned out a headache for me.
I managed to create a module that resolves MainRouter, inject it into an activity, but it doesn't work for a fragment.
MainActivity extends ScopeActivity, MyFragment extends ScopeFragment.
in MainActivity
private val router : MainRouter by inject()
works fine, but in MyFragment it throws org.koin.core.error.NoBeanDefFoundException: No definition found for class:'com.example.app.MainRouter'. Check your definitions!Finally I managed to inject, but it doesn't look pretty
I also don't like that
scopeActivity
can't be accessed in the init method. Does this mean that activity scoped dependencies cannot be resolved in fragment usingby inject()
?The text was updated successfully, but these errors were encountered: