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 abstract class FragmentInjectActivity extends AppCompatActivity | |
implements HasAndroidInjector { | |
@Inject DispatchingAndroidInjector<Object> androidInjector; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
AndroidInjection.inject(this); | |
beforeOnCreate(); // <-- expose here for child to do things before onCreate | |
super.onCreate(savedInstanceState); |
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 MainActivity : DaggerAppCompatActivity() { | |
@Inject lateinit var fragmentFactory: FragmentFactory | |
override fun onCreate(savedInstanceState: Bundle?) { | |
supportFragmentManager.fragmentFactory = fragmentFactory | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
} | |
} |
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 | |
abstract class DummyBuilder { | |
@ContributesAndroidInjector(modules = [DummyModule::class]) | |
abstract fun bindDummygFragment(): DummyFragment | |
// put binding of fragment at this layer(not DummyModule) for parent to inject | |
@Binds | |
@IntoMap | |
@FragmentKey(DummyFragment::class) | |
abstract fun bindDummyFragment(fragment: DummyFragment): 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
@Module | |
abstract class MainBuilder { | |
@ContributesAndroidInjector(modules = [ | |
ViewModelBuilder::class, | |
FragmentBuilder::class, // <- for injection of fragment factory | |
MainModule::class, | |
DummyBuilder::class // <- our fragment | |
]) | |
abstract fun bindMainActivity(): MainActivity | |
} |
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 | |
abstract class FragmentBuilder { | |
@Binds | |
abstract fun bindFragmentFactory( | |
factory: DaggerFragmentFactory | |
): 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
@Target( | |
AnnotationTarget.FUNCTION, | |
AnnotationTarget.PROPERTY_GETTER, | |
AnnotationTarget.PROPERTY_SETTER | |
) | |
@Retention(AnnotationRetention.RUNTIME) | |
@MapKey | |
annotation class FragmentKey(val value: KClass<out 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
class DaggerFragmentFactory @Inject constructor( | |
private val providers: @JvmSuppressWildcards Map<Class<out Fragment>, Provider<Fragment>> | |
) : FragmentFactory() { | |
override fun instantiate(classLoader: ClassLoader, className: String): Fragment { | |
val fragmentClass = loadFragmentClass(classLoader, className) | |
val provider = providers[fragmentClass] | |
return provider?.get() ?: super.instantiate(classLoader, className) | |
} | |
} |
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 DummyFragment @Inject constructor( | |
private val repo: AppRepo | |
) : DaggerFragment() { | |
@Inject | |
lateinit var vmFactory: ViewModelProvider.Factory | |
private val viewModel by viewModels<DummyViewModel> { vmFactory } | |
// ... | |
} |
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 MainActivity: FragmentInjectActivity() { | |
@Inject lateinit var fragmentFactory: FragmentFactory | |
override fun onCreate(savedInstanceState: Bundle?) { | |
supportFragmentManager.fragmentFactory = fragmentFactory | |
super.onCreate(savedInstanceState) | |
// launch fragment and add to fragmen container view | |
supportFragmentManager.beginTransaction() | |
.add<DummyFragment>(R.id.fragmentContainer) | |
.commit() |
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
package com.diousk.serializationsample | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.lifecycle.lifecycleScope | |
import com.diousk.serializationsample.model.MockData | |
import com.diousk.serializationsample.model.mockDataList | |
import com.diousk.serializationsample.model.mockJsonString | |
import com.diousk.serializationsample.parser.GsonParser |