title: how to stop observer to be triggered more than once when viewmodel holds value from client?
body: <p>disclaimer, this is different question from <a href="https://stackoverflow.com/questions/60751087/how-to-stop-observer-to-automatically-observe-data-when-pop-back-to-previous-fra?noredirect=1#comment107485594_60751087">my previous question</a></p>
  
  <p>I am new in MVVM and I am little bit confused. </p>
  
  <p>so If a user clicks the button in my fragment, then I will make a request to server to check if this user has created a music event or not, it he has not created a music event then move to Fragment B. here is my FragmentA</p>
  
  <pre><code>class FragmentA : Fragment() {
  
      lateinit var model: AViewModel
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
  
          model = ViewModelProvider(this).get(AViewModel::class.java)
  
  
          button.setOnClickListener {
              model.checkIfUserHasCreatedEvent()
          }
  
  
          model.hasCreatedEvent.observe(this, Observer { hasCreatedEvent -&gt;
  
              if (!hasCreatedEvent) {
                  val chooseEventNameDestination = CreateEventFragmentDirections.actionToCreateEventName()
                  findNavController().navigate(chooseEventNameDestination)
                  model.navigationHasBeendHandled()
              }
  
          })
  
  
  
      }
  
  }
  </code></pre>
  
  <p>and here is the viewmodel for this fragment</p>
  
  <pre><code>class AViewModel(application: Application) : AndroidViewModel(application) {
  
      val _hasCreatedEvent : MutableLiveData&lt;Boolean&gt; = UserClient.hasCreatedEvent
      val hasCreatedEvent : LiveData&lt;Boolean&gt;
         get() = _hasCreatedEvent
  
      fun checkIfUserHasCreatedEvent() {
          UserClient.checkIfUserHasCreatedAnEvent()
  
      }
  
      fun navigationHasBeendHandled() {
          _hasCreatedEvent.value = true // to reset data, to avoid the value to be false
      }
  
  
  }
  </code></pre>
  
  <p>and here is the UserClient (I skip the repository for simplicity)</p>
  
  <pre><code>object UserClient {
  
      val hasCreatedEvent = MutableLiveData&lt;Boolean&gt;()
  
      fun checkIfUserHasCreatedAnEvent() {
  
          // perform networking to server ....
          // and post the value after getting the response
  
          hasCreatedEvent.postValue(result)
  
      }
  
  
  }
  </code></pre>
  
  <p>when I first click the button, it works as expected, it moves from fragment A to fragment B. because after checking to the server, <code>hasCreatedEvent == false</code>.</p>
  
  <p>the problem occurs when I back from fragment B to fragment A.</p>
  
  <p>when get back from fragment B, the observer in fragment A is triggered more than once, even though I am not click the button, it is automatically triggered.</p>
  
  <pre><code>    model.hasCreatedEvent.observe(this, Observer { hasCreatedEvent -&gt;
  
          // triggered 3 times in here 
  
          Log.d("AViewModel","result: $hasCreatedEvent")
  
          // result: true
          // result: false  &lt;--- the problem
          // result: true
  
          if (!hasCreatedEvent) {
              // because there is a false value ...
              // then this block is triggered, and it makes it moves to fragmentB again
          }
  
  
  
      })
  </code></pre>
  
  <p>I believe the <code>true</code> value comes from <code>navigationHasBeendHandled</code> method from my viewModel,  but I don't understand why there is <code>false</code> value again when I get back from fragment B to fragment A ? even though I don't click the button </p>
  
  <p>I assume <code>false</code> value comes from <code>UserClient.hasCreatedEvent</code> that holds value after making request from server.</p>
  
  <p>but I don't know how to solve this issue, because that <code>false</code> value makes it moves automatically to fragment B again.</p>
  
  <p>java or kotlin are okay</p>