Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用navigatio和dagger2制作片段单例?_Android - Fatal编程技术网

Android 如何使用navigatio和dagger2制作片段单例?

Android 如何使用navigatio和dagger2制作片段单例?,android,Android,我正在使用dagger2进行碎片导航。我需要创建一个片段singleton。虽然我在ApplicationComponent中添加了@singleton注释,但它不起作用。为了制作碎片单体,我必须放弃使用dagger2和碎片导航?在测试生命周期时,我的片段多次被称为“onActivityCreated”。我无法控制替换片段,因为我使用导航图。所以我不能用片段替换 家庭片段 应用模块 @Singleton @组成部分( 模块=[ ApplicationModule::类, AndroidSuppo

我正在使用dagger2进行碎片导航。我需要创建一个片段singleton。虽然我在ApplicationComponent中添加了@singleton注释,但它不起作用。为了制作碎片单体,我必须放弃使用dagger2和碎片导航?在测试生命周期时,我的片段多次被称为“onActivityCreated”。我无法控制替换片段,因为我使用导航图。所以我不能用片段替换

家庭片段

应用模块

@Singleton
@组成部分(
模块=[
ApplicationModule::类,
AndroidSupportInjectionModule::类,
RegisterModule::类,
HomeModule::类,
第二个模块::类
])
接口应用程序组件:Androidjector{
@组件工厂
接口工厂{
趣味创建(@BindsInstance applicationContext:Context):ApplicationComponent
}
}
导航图



如果您需要将片段设置为单例,那么您正在做一些非常错误的事情,因为系统稍后会重新创建它,因此这样做是行不通的。要将片段设置为单例,我是否必须放弃dagger2和导航?您根本无法将片段设置为单例。如果您需要将片段设置为单例,你正在做一些非常错误的事情,因为系统稍后会重新创建它,因此这不会以那种方式工作。要生成碎片单体,我必须放弃dagger2和导航吗?你根本无法将碎片生成单体。
class HomeFragment : DaggerFragment() {
    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory
    private val viewModel by viewModels<HomeViewModel> { viewModelFactory }
    private lateinit var viewDataBinding: FragmentHomeBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_home, container, false)
        viewDataBinding = FragmentHomeBinding.inflate(inflater, container, false).apply {
            viewmodel = viewModel
        }
        return view
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        viewDataBinding.lifecycleOwner = this.viewLifecycleOwner
        Log.d("TAG","HomeFragment onCreate")
        init()
        viewModel.getNotice()
    }

    private fun init(){
        viewModel.notice.observe(this, Observer{
            noticeMain.text = it

        })
    }

}

@Module
abstract class HomeModule {

    @ContributesAndroidInjector(modules = [
        ViewModelBuilder::class
        ])


    internal abstract fun homeFragment(): HomeFragment

    @Binds
    @IntoMap
    @ViewModelKey(HomeViewModel::class)
    abstract fun bindViewModel(viewmodel: HomeViewModel): ViewModel
}

@Singleton
@Component(
    modules = [
        ApplicationModule::class,
        AndroidSupportInjectionModule::class,
        RegisterModule::class,
        HomeModule::class,
        SecondModule::class
    ])
interface ApplicationComponent : AndroidInjector<GlobalApplication> {

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance applicationContext: Context): ApplicationComponent
    }
}

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@+id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.haii.loginproject.fragment.home.HomeFragment"
        android:label="Home"
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/action_homeFragment_to_secondFragment"
            app:destination="@id/secondFragment" />

    </fragment>

    <fragment
        android:id="@+id/secondFragment"
        android:name="com.haii.loginproject.fragment.second.SecondFragment"
        android:label="Second"
        tools:layout="@layout/fragment_second">
        <action
            android:id="@+id/action_secondFragment_to_homeFragment"
            app:destination="@id/homeFragment" />

    </fragment>
</navigation>