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
@Module | |
interface ActivityBindsModule { | |
@Binds | |
@IntoMap | |
@ActivityKey(MainActivity::class) | |
fun bindMainActivityToActivityForMultiBinding(activity: MainActivity): Activity | |
} |
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
@Module | |
interface ComponentProvidersModule { | |
// Define that a Dagger Component can provide Multibind Map of Activities by class | |
@Multibinds | |
fun provideActivities(): Map<Class<out Activity>, @JvmSuppressWildcards Activity> | |
} |
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
@Module | |
object AppModule { | |
@Provides | |
fun provideFragmentFactory( | |
fragmentProviders: Map<Class<out Fragment>, @JvmSuppressWildcards Provider<Fragment>> | |
): FragmentFactory { | |
return InjectFragmentFactory(fragmentProviders) | |
} | |
} |
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
@Component( | |
modules = [ | |
AppModule::class, | |
ComponentProvidersModule::class, | |
FragmentBindsModule::class, | |
ActivityBindsModule::class | |
] | |
) | |
interface AppComponent { |
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 SampleApplication : Application() { | |
lateinit var appComponent: AppComponent | |
override fun onCreate() { | |
super.onCreate() | |
appComponent = DaggerAppComponent.create() | |
registerActivityLifecycleCallbacks( | |
SetFragmentFactoryActivityCallback(appComponent.fragmentFactory) | |
) |
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 SetFragmentFactoryActivityCallback( | |
private val newFragmentFactory: FragmentFactory | |
) : EmptyActivityLifecycleCallbacks { | |
private val fragmentLifecycleCallbacks = | |
SetFragmentFactoryFragmentCallback(newFragmentFactory) | |
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | |
if (activity is FragmentActivity) { | |
val fragmentManager = activity.supportFragmentManager |
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
<manifest | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
> | |
<application | |
android:name=".SampleApplication" | |
android:appComponentFactory="com.kirich1409.android.inject.InjectComponentFactory" | |
tools:replace="android:appComponentFactory" | |
> |
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 InjectFragmentFactory( | |
private val providers: Map<String, Provider<Fragment>> | |
) : FragmentFactory() { | |
override fun instantiate(classLoader: ClassLoader, className: String): Fragment { | |
return providers[className]?.get() ?: super.instantiate(classLoader, className) | |
} | |
companion object { |
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
@RequiresApi(Build.VERSION_CODES.P) | |
// We still need default constructor for AppComponentFactory | |
class InjectComponentFactory : AppComponentFactory() { | |
private lateinit var application: SampleApplication | |
private val activityProviders: Map<String, Provider<Activity>> by lazy { | |
application.appComponent.activityProviders | |
.mapKeys { (key, _) -> key.name } | |
} |
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 MyActivity @Inject constructor( | |
private val dependency: Dependency | |
) : AppCompatActivity() | |
class MyFragment @Inject constructor( | |
private val dependency: Dependency | |
) : Fragment() |