片段事务后不会删除LiveData观察者,并且从后台添加片段时会调用onChange方法

时间:2019-04-12 22:01:37

标签: android kotlin android-livedata

我在一个片段中创建一个点击监听器。该侦听器在视图模型中设置一个livedata值。我正在观察该值,当它改变时,我将其值放在一个包中,并在一个片段事务中将其传递给下一个片段。

一切正常。但是,当我浏览该片段(使用“后退”按钮)时,观察者再次被调用,并且它导航离开。

我尝试过:  -如本文所示,使用扩展功能删除观察者.. https://code.luasoftware.com/tutorials/android/android-livedata-observe-once-only-kotlin/

  • 在关闭观察者之后将viewmodel和listener设置为null

-尝试重置实时数据字符串的值


设置侦听器:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
 super.onViewCreated(view, savedInstanceState)
        val backpackImage = view?.findViewById(R.id.image_backpack) as ImageView
        val  poloImage = view?.findViewById(R.id.polo_image) as ImageView
        val pantsImage = view?.findViewById(R.id.pants_image) as ImageView
        val socksImage = view?.findViewById(R.id.socks_image) as ImageView
        val sweatshirtImage = view?.findViewById(R.id.sweatshirt_image) as ImageView

        categoryOfImageClickListner = View.OnClickListener {
                view->

            when(view.id){

                R.id.image_backpack -> viewModel?.setCategory(Categories.BACKPACKS.storeCategories)

                R.id.polo_image ->viewModel?.setCategory(Categories.POLOS.storeCategories)
                R.id.pants_image ->viewModel?.setCategory(Categories.PANTS.storeCategories)
                R.id.socks_image ->viewModel?.setCategory(Categories.SOCKS.storeCategories)
                R.id.sweatshirt_image ->viewModel?.setCategory(Categories.SWEATSHIRTS.storeCategories)
            }
        }
        backpackImage.setOnClickListener(categoryOfImageClickListner)
        socksImage.setOnClickListener(categoryOfImageClickListner)
        pantsImage.setOnClickListener(categoryOfImageClickListner)
        poloImage.setOnClickListener(categoryOfImageClickListner)
        sweatshirtImage.setOnClickListener(categoryOfImageClickListner)

    }
---------------------

观察者:

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

 var categoryObservable :LiveData<String> = viewModel?.getCategoryForNavigation()!!

            categoryObservable.observeOnce(viewLifecycleOwner, Observer {

                categoryObservable.removeObservers(this)
                it.let {
                    var args = Bundle()
                    args.putString("CATEGORY", it)
                    val productFrag = ProductListFragment()

                    productFrag.arguments = args


                    val fragmentManger = activity?.let {

                        if (it is FragmentActivity)
                            it.supportFragmentManager.beginTransaction()
                                .replace(R.id.fragment_container, productFrag, "PRODUCTFRAG")
                                .addToBackStack(null)
                                .commit()


                    }


                }


            })

````````````
the
Viewmodel
````````````
class HomeViewModel : ViewModel() {



  var liveDataCategory: MutableLiveData<String> = MutableLiveData()

    fun getCategoryForNavigation(): LiveData<String> =liveDataCategory  // observe this in fragment.. and nav onChange
    fun ResetCategoryForNav()= liveDataCategory.postValue(Categories.SWEATSHIRTS.storeCategories)
    fun setCategory( category: String){
        liveDataCategory.postValue(category)
        Log.d("ONCLICK", "Inside Onclick Viewmodel")
    }

0 个答案:

没有答案