1

I am using the latest Koin version 3.1.2. I have a basic Fragment that accepts a String parameter to it's constructor.

My Koin setup is as follows:

Activity:

override val scope: Scope by activityScope()
private val fragment by inject<MyFragment> {
    parametersOf(intent.getStringExtra(PROJECT_ID_EXTRA))
}

override fun onCreate(savedInstanceState: Bundle?) {
    setupKoinFragmentFactory(scope)
    super.onCreate(savedInstanceState)
    .
    .
}

Fragment:

class MyFragment(private val projectId: String?) : BaseFragment() {

}

Koin Module:

scope<MyActivity> {
    fragment { (projectId: String) -> MyFragment(projectId) }
}

To simulate activity re-creation, I have turned on Don't keep activities flag in the developer options.

What always happens when activity is re-created, I get the following exception:

Unable to start activity ComponentInfo{com.activity.MyActivity}: org.koin.core.error.InstanceCreationException: Could not create instance for [Factory:'com.fragment.MyFragment',scope:q:'com.activity.MyActivity']
Can't get injected parameter #0 from DefinitionParameters[] for type 'java.lang.String'
TareK Khoury
  • 12,721
  • 16
  • 55
  • 78
  • Fragments should have empty constructor, this has nothing to do with Koin. Read here: https://stackoverflow.com/questions/10450348/do-fragments-really-need-an-empty-constructor – Torkel Velure Jul 06 '21 at 13:28
  • Does this answer your question? [Do fragments really need an empty constructor?](https://stackoverflow.com/questions/10450348/do-fragments-really-need-an-empty-constructor) – Torkel Velure Jul 06 '21 at 13:33
  • @TorkelVelure What you are saying is true if I wasn't using Fragment Factory. In my case I should not need an empty constructor – TareK Khoury Jul 11 '21 at 11:21

0 Answers0