Created
February 27, 2018 16:04
-
-
Save arnaudgiuliani/446e0d8ee79f4bda38b3057b01098f2e to your computer and use it in GitHub Desktop.
Koin for Android Java app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyApplication extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
// Call helper to start Koin | |
JavaAppKoinKt.start(this); | |
} | |
} | |
public class MainActivity extends AppCompatActivity { | |
MainActivityHolder holder = new MainActivityHolder(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
//... | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
//... | |
// Use my components here | |
holder.getJComp().sayHello(); | |
holder.getKComp().sayHello(); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A module with Kotlin and Java components | |
val module = applicationContext { | |
bean { KotlinComponent() } | |
bean { JavaComponent() } | |
} | |
// Start | |
fun start(myApplication: Application) { | |
// Start Koin with given Application instance | |
startKoin(listOf(module)) with (myApplication) | |
} | |
// Dependency holder | |
class MainActivityHolder : KoinComponent { | |
val kComp: KotlinComponent by inject() | |
val jComp: JavaComponent by inject() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JavaComponent { | |
public void sayHello() { | |
System.out.println("Hello Koin from Java"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KotlinComponent { | |
fun sayHello() = println("Hello Koin from Kotlin") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍