question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi,

Koin is awesome but I’m having an issue. Some libraries, like Firebase Analytics, depend on the current Activity to perform some actions like setting a current screen.

class Analytics(private val activity: Activity) {

  private val firebaseAnalytics: FirebaseAnalytics = FirebaseAnalytics.getInstance(activity)

  fun setCurrentScreen(screenName: String) {
    firebaseAnalytics.setCurrentScreen(activity, screenName, null)
  }
}
class MainPresenter(analytics: Analytics) { 
    
}
class MainActivity : AppCompatActivity() {

  val presenter : MainPresenter by inject()

}

Is this possible with Koin? If yes, then I don’t see how.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:30 (11 by maintainers)

github_iconTop GitHub Comments

29reactions
dgngulcancommented, Aug 20, 2018

I am using parameters like below @westonal.

LocationModule.class

factory { (activity: MainActivity) -> FusedLocationProviderClient(activity) }

MainActivity.class

val provider: FusedLocationProviderClient by inject { parametersOf(this@MainActivity) }
29reactions
arnaudgiulianicommented, Feb 22, 2018

I 've add Koin parameters to release 0.9.0.

you will be able to use parameters in your definition. Given class:

class MyPresenter(val activity : MyActivity)

We can use parameters to be injected with by inject()

val module = applicationContext {
   factory { params -> MyPresenter(params["activity"])}
}

Injecting the parameter:

class MyActivity : AppCompatActivity(){
   
   // Ask for MyPresenter injection and provide parameters
   val presenter : MyPresenter by inject( parameters = mapOf("activity" to this))
}

stay tuned👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inject activity using dagger - java - Stack Overflow
This way Dagger can create a singleton Picasso instance. The ReAdapter is annotated with @Inject (using constructor injection) so Dagger knows ...
Read more >
Using Dagger in Android apps
To inject an object in the activity, you'd use the appComponent defined in your Application class and call the inject() method, passing in...
Read more >
How to use Android Injector for Activity and Fragment objects ...
How to use Android Injector for Activity and Fragment objects through ... We can use AndroidInjection.inject(this) in activity after inject ...
Read more >
Dagger: When to Inject for Activity and Fragment - Sangsoo Nam
For the dependency injection on Activity and Fragment , Dagger supports a helper method, AndroidInjection.inject() . Once it is called, ...
Read more >
Dagger & Android Thoughts: Dependency Injection in Android
From the Dagger documentation — Many Android framework classes are instantiated by the OS itself, like Activity and Fragment. You have to perform...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found