[英]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
:我有底部栏的活动,它切换两个片段
galleryFrament
和profilefragment
,它们被注入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
和@ActivityScoped
: https : //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.