简体   繁体   English

只要有活动,Android Dagger 2 就会保持片段活着

[英]Android Dagger 2 keep fragment alive as long as activity

I have activity with bottom bar, that switches two fragments galleryFrament and profilefragment , that are injected into MainActivity :我有底部栏的活动,它切换两个片段galleryFramentprofilefragment ,它们被注入MainActivity

class MainActivity : DaggerAppCompatActivity(), MainContract.View {

@Inject
lateinit var galleryFragment: GalleryFragment

@Inject
lateinit var profileFragment: ProfileFragment

I switch between them in this way:我以这种方式在它们之间切换:

val transaction = fragmentManager.beginTransaction()
transaction.replace(frameId, fragment)
transaction.commit()

Module that provides MainActivity , with Scope @ActivityScoped :提供MainActivity和 Scope @ActivityScoped

  @Module
  abstract class ActivityBindingModule {

    @ActivityScoped
    @ContributesAndroidInjector(modules = arrayOf(MainModule::class, GalleryModule::class, ProfileModule::class))
    internal abstract fun mainActivity(): MainActivity

Module that provides GalleryFragment :提供GalleryFragment模块:

  @Module
internal abstract class GalleryModule {

    @FragmentScoped
    @ContributesAndroidInjector
    abstract fun galleryFragment(): GalleryFragment

    @ActivityScoped
    @Binds
    abstract fun galleryPresenter(galleryPresenter: GalleryPresenter): GalleryContract.Presenter


    @Module
    companion object {
        @ActivityScoped
        @Provides
        @JvmStatic
        fun galleryAdapter(context: Context): GalleryAdapter {
            return GalleryAdapter(context);
        }
    }
}

And, finaly AppComponent :最后, AppComponent

@Singleton
@Component(modules = arrayOf(ImagesRepositoryModule::class,
        ApplicationModule::class,
        ActivityBindingModule::class,
        AndroidSupportInjectionModule::class,
        EventsModule::class))
interface AppComponent : AndroidInjector<MyApplication> {

GalleryFragment is provided with @FragmentScoped annotation from GaleryModule . GalleryFragment提供了来自GaleryModule @FragmentScoped注释。 I took aproach with annotatins @FragmentScoped and @ActivityScoped from google blueprints samples posted here: https://github.com/googlesamples/android-architecture][google-archutecture我从这里发布的谷歌蓝图样本中使用了注释@FragmentScoped@ActivityScopedhttps : //github.com/googlesamples/android-architecture][ google- @ActivityScoped

The probem is: When I switch fragments, the new instance of GalleryFrament is created ( onCreate() called every time), but I want to use previously created instance of it.问题是:当我切换片段时,会创建GalleryFrament的新实例onCreate()每次都调用onCreate() ),但我想使用它以前创建的实例。 As I understand dagger scopes, GalleryFragment should be @ActivityScoped (to keep it alive as long as activity lives), but if I set it, @ContributesAndroidInjector produces Subcomponent with the same scope as its parent, and that is illegal.据我了解 dagger 范围, GalleryFragment应该是@ActivityScoped (只要活动存在就让它保持活动状态),但是如果我设置它, @ContributesAndroidInjector会生成与父组件具有相同范围的子组件,这是非法的。

If you want to have your fragment in your activity scope you mustn't create fragment subcomponent with @ContributesAndroidInjector so instead of如果你想在你的活动范围内拥有你的片段,你不能使用@ContributesAndroidInjector创建片段子组件,而不是

@FragmentScoped
@ContributesAndroidInjector
abstract fun galleryFragment(): GalleryFragment

try to move it in your companion like so试着像这样在你的同伴中移动它

@Provides
@ActivityScoped
@JvmStatic
fun galleryFragment(): GalleryFragment = GalleryFragment.newInstance()

or remove it and from your GalleryFragment class或从您的 GalleryFragment 类中删除它

@ActivityScope
class GalleryFragment 
@Inject
constructor() : Fragment()

Though i wouldn't recommanded to do so, why would you want to have fragments matching your activity lifecycle?虽然我不建议这样做,但为什么要让 Fragment 与您的 Activity 生命周期相匹配?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-将Dagger注入到非Activity或片段中 - Android - Dagger injection into a non Activity or fragment 使用WebView保持活动状态 - Keep alive Activity with WebView 无法从Dagger(Android)中的片段组件访问活动组件 - Unable to access Activity Component from Fragment Component in Dagger(Android) Android上的Dagger 2:在Activity中注入相同的依赖项并保留Fragment - Dagger 2 on Android: inject same dependency in Activity and retained Fragment Dagger 2将片段注入活动-错误:[Dagger / MissingBinding] - Dagger 2 Injecting Fragment into Activity - error: [Dagger/MissingBinding] Dagger 2创建的对象在Android中保留多长时间/ Dagger 2如何与Android Activity生命周期一起玩? - How long do objects created by Dagger 2 remain in Android / how does Dagger 2 play with the Android Activity lifecycle? 如何在 android 上长时间保持 inte.net 连接? - How to keep internet connection alive for a long time on android? android:Activity需要最小化或隐藏,如何保持活着 - android: Activity needs to be minimized or hidden, how to keep it alive C#Android在活动更改时使线程保持活动状态 - C# Android Keep thread alive on Activity Change 如何在关闭android中的所有其他活动的同时保持活动? - How to keep an activity alive while closing all the others in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM