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 : AppCompatActivity() { | |
@Inject | |
lateinit var dependency: Dependency | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCrate(savedInstanceState) | |
getAppComponent().inject(this) | |
} |
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 MyFragment : Fragment() { | |
@Inject | |
lateinit var dependency: Dependency | |
override fun onAttach(context: Context) { | |
super.onAttach(context) | |
getAppComponent().inject(this) | |
} |
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() |
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 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
<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 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
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
@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
@Module | |
object AppModule { | |
@Provides | |
fun provideFragmentFactory( | |
fragmentProviders: Map<Class<out Fragment>, @JvmSuppressWildcards Provider<Fragment>> | |
): FragmentFactory { | |
return InjectFragmentFactory(fragmentProviders) | |
} | |
} |