1

I have a BaseActivity which has a MutableLiveData field as below

val refInfoLiveData: MutableLiveData<RefInfo?> by lazy { MutableLiveData<RefInfo?>() }

A network call is made to populate this MutableLiveData field when onStart method of the BaseActivity is called.

I also have a couple of Fragments which are parts of an Activity that inherits the BaseActivity.

In one if these fragments I am making another call in onResume method of the fragment as below

    (activity as BaseActivity).refInfoLiveData.observe(this, Observer {
        it?.let { refInfo ->
            adapter?.setRefInfo(refInfo)
        }
    })

When the fragment is created for the first time observe only called once but the fragment goes to background then comes back it is called multiple times and that is causing issues.

What could be the reason of this problem and how can I solve it?

Any help would be appreciated.

Tartar
  • 5,149
  • 16
  • 63
  • 104
  • check this thread : https://stackoverflow.com/questions/50236778/why-livedata-observer-is-being-triggered-twice-for-a-newly-attached-observer – diAz Jun 06 '19 at 15:25
  • @ZachBvy that is a different problem, only applicable to query results obtained from Room. – EpicPandaForce Jun 06 '19 at 15:40

2 Answers2

4

That's because you are supposed to be using observe(viewLifecycleOwner, Observer { ... inside onViewCreated.

import androidx.lifecycle.observe

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    (activity as BaseActivity).refInfoLiveData.observe(viewLifecycleOwner) { refInfo ->
        refInfo?.let { adapter.setRefInfo(it) }
    }
}

Currently you'll have an infinite number of subscribers if you put the app in background then bring it foreground an infinite number of times.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

https://www.javatpoint.com/android-life-cycle-of-activity

Maybe you could try to use another lifecyclehook, because onResume() is called most often of the entry hooks.

Try it in the onCreate().

Preacher
  • 2,127
  • 1
  • 11
  • 25
d4ku3_
  • 1
  • 4
  • Moving it to onActivityCreated would solve that problem but in my case it creates different problems so I kind of need to keep it in onResume – Tartar Jun 06 '19 at 15:34
  • Don't use `onActivityCreated`, there is no reason to use it in any scenarios that I know of – EpicPandaForce Jun 06 '19 at 15:40
  • @Tartar, if you want to keep it in `onResume`, you need to implement your own `LifeCycleOwner` instead of using the default `viewLifecycleOwner`. – Sanlok Lee Jun 06 '19 at 17:43
  • @d4ku3, please include some relevant content from the linked page, in case the link gets broken. It would also be helpful to tell readers what they will find by following the link. – Preacher Jun 07 '19 at 00:26
  • @Preacher sure, maybe you could tell me how to include pics ^^, wanted to show the lifecycle diagram – d4ku3_ Jun 07 '19 at 07:26
  • @d4ku3 I don't know, off hand. I'd have to go figure it out. But in lieu, you may be able to present the diagram as a sequence of steps, having the last step being "repeat" to show the cyclic nature. – Preacher Jun 07 '19 at 14:37