title: Live data giving old value again while changing fragment
body: <p>I am using live data from a shared ViewModel across multiple fragments. I have a sign-in fragment which takes user's phone number and password and then the user presses sign in button I am calling the API for that, now if the sign-in fails I am showing a toast "Sign In failed", now if the user goes to "ForgotPassword" screen which also uses the same view model as "SignInFragment" and presses back from the forgot password screen, it comes to sign-in fragment, but it again shows the toast "Sign In failed" but the API is not called, it gets data from the previously registered observer, so is there any way to fix this?</p>
  
  <p>SignInFragment.kt</p>
  
  <pre><code>class SignInFragment : Fragment() {
  
      private lateinit var binding: FragmentSignInBinding
  
      //Shared view model across two fragments
      private val onBoardViewModel by activityViewModels&lt;OnBoardViewModel&gt;()
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          binding = DataBindingUtil.inflate(
              inflater,
              R.layout.fragment_sign_in,
              container,
              false
          )
          return binding.root
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
          onBoardViewModel.signInResponse.observe(viewLifecycleOwner) { response -&gt;
              //This is calling again after coming back from new fragment it.
              showToast("Sign In Failed")
          }
      }
  
      override fun onClick(v: View?) {
          when (v?.id!!) {
              R.id.forgotPasswordTV -&gt; {
                  findNavController().navigate(SignInFragmentDirections.actionSignInFragmentToForgotPasswordFragment())
              }
              R.id.signInTV -&gt; {
                  val phoneNumber = binding.phoneNumberET.text
                  val password = binding.passwordET.text
                  val signInRequestModel = SignInRequestModel(
                      phoneNumber.toString(),
                      password.toString(),
                      ""
                  )
  
                  //Calling API for the sign-in
                  onBoardViewModel.callSignInAPI(signInRequestModel)
              }
          }
      }
  }
  </code></pre>
  
  <p>ForgotPasswordFragment </p>
  
  <pre><code>class ForgotPasswordFragment : Fragment() {
  
      private lateinit var binding: FragmentForgotPasswordBinding
  
      //Shared view model across two fragments
      private val onBoardViewModel by activityViewModels&lt;OnBoardViewModel&gt;()
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          binding = DataBindingUtil.inflate(
              inflater,
              R.layout.fragment_forgot_password,
              container,
              false
          )
          return binding.root
      }
  }
  </code></pre>
  
  <p>OnBoardViewModel</p>
  
  <pre><code>class OnBoardViewModel : ViewModel() {
  
      private var repository: OnBoardRepository = OnBoardRepository.getInstance()
  
      private val signInRequestLiveData = MutableLiveData&lt;SignInRequestModel&gt;()
  
      //Observing this data in sign in fragment
      val signInResponse: LiveData&lt;APIResource&lt;SignInResponse&gt;&gt; =
          signInRequestLiveData.switchMap {
              repository.callSignInAPI(it)
          }
  
      //Calling this function from sign in fragment
      fun callSignInAPI(signInRequestModel: SignInRequestModel) {
          signInRequestLiveData.value = signInRequestModel
      }
  
      override fun onCleared() {
          super.onCleared()
          repository.clearRepo()
      }
  }
  </code></pre>
  
  <p>I have tried to move this code inside onActivityCreated but it's still getting called after coming back from new fragment.</p>
  
  <pre><code> onBoardViewModel.signInResponse.observe(viewLifecycleOwner) { response -&gt;
              showToast("Sign In Failed")
          }
  </code></pre>