Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
LiveData is observed multiple times inside onClickListener in Android

I have a repository setup like this

class ServerTimeRepo @Inject constructor(private val retrofit: Retrofit){
    var liveDataTime = MutableLiveData<TimeResponse>()
    fun getServerTime(): LiveData<TimeResponse> {
        val serverTimeService:ServerTimeService = retrofit.create(ServerTimeService::class.java)
        val obs = serverTimeService.getServerTime()
        obs.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
                .subscribe(object : Observer<Response<TimeResponse>> {
                    override fun onComplete() {

                    }

                    override fun onSubscribe(d: Disposable) {

                    }

                    override fun onNext(t: Response<TimeResponse>) {
                        val gson = Gson()
                        val json: String?
                        val code = t.code()
                        val cs = code.toString()
                        if (!cs.equals("200")) {
                            json = t.errorBody()!!.string()
                            val userError = gson.fromJson(json, Error::class.java)
                        } else {

                            liveDataTime.value = t.body()
                        }

                    }

                    override fun onError(e: Throwable) {

                    }

                })
        return liveDataTime
    }
}

Then I have a viewmodel calling this repo like this

class ServerTimeViewModel @Inject constructor(private val serverTimeRepo: ServerTimeRepo):ViewModel() {

    fun getServerTime(): LiveData<TimeResponse> {
        return serverTimeRepo.getServerTime()
    }
}

Then I have an activity where I have an onClickListener where I am observing the livedata, like this

tvPWStart.setOnClickListener {
         val stlv=  serverTimeViewModel.getServerTime()

            stlv.observe(this@HomeScreenActivity, Observer {
                //this is getting called multiple times??

            })
        }

I don't know what's wrong in this. Can anyone point me in the right direction? Thanks.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Issue is that every time your ClickListener gets fired, you observe LiveData again and again. So, you can solve that problem by following solution :

  1. Take a MutableLiveData object inside your ViewModel privately & Observe it as LiveData.

    class ServerTimeViewModel @Inject constructor(private val serverTimeRepo: ServerTimeRepo):ViewModel() {
    
        private val serverTimeData = MutableLiveData<TimeResponse>() // We make private variable so that UI/View can't modify directly
    
        fun getServerTime() {
            serverTimeData.value = serverTimeRepo.getServerTime().value // Rather than returning LiveData, we set value to our local MutableLiveData
        }
    
        fun observeServerTime(): LiveData<TimeResponse> {
            return serverTimeData //Here we expose our MutableLiveData as LiveData to avoid modification from UI/View
        }
    }
    
  2. Now, we observe this LiveData directly outside of ClickListener and we just call API method from button click like below :

    //Assuming that this code is inside onCreate() of your Activity/Fragment
    //first we observe our LiveData
    serverTimeViewModel.observeServerTime().observe(this@HomeScreenActivity, Observer {
            //In such case, we won't observe multiple LiveData but one
    })
    //Then during our ClickListener, we just do API method call without any callback.
    tvPWStart.setOnClickListener {
        serverTimeViewModel.getServerTime()
    }
    
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!