title: Can't Observe LiveData from ViewModel in Fragment
body: <p>I have a <code>RecyclerView</code> in my <code>ViewPager</code>'s one of the <code>Fragment</code>. This <code>RecyclerView</code> show's data that are fetched from Server. It normally works, but I couldn't be able to implement the same feature using <code>ViewModel</code> and <code>LiveData</code>, because the <code>livedata.observe</code> method is not getting called when the <code>livedata</code> is being changed from the <code>ViewModel</code>.</p>
  
  <p>Here's the Source Code of my <strong>MonthlyOrderViewModel.java</strong> class</p>
  
  <pre><code>public class MonthlyOrderViewModel extends AndroidViewModel {
  
  private Retrofit retrofit;
  private RetrofitServices retrofitServices;
  private MutableLiveData&lt;MonthlyOrderHistory&gt; monthlyOrderHistoryMutableLiveData = new MutableLiveData&lt;&gt;();
  
  public MonthlyOrderViewModel(@NonNull Application application) {
      super(application);
      retrofit = RetrofitFactory.getRetrofit();
      retrofitServices = retrofit.create(RetrofitServices.class);
  }
  
  public MutableLiveData&lt;MonthlyOrderHistory&gt; getMonthlyOrderHistoryMutableLiveData() {
      return monthlyOrderHistoryMutableLiveData;
  }
  public void fetchMonthlyOrders() {
      Call&lt;MonthlyOrderHistory&gt; call = retrofitServices.getAllMonthlyOrderHistory(IpharmaApplication.getInstance().getLoggedInUser().getId());
      call.enqueue(new Callback&lt;MonthlyOrderHistory&gt;() {
          @Override
          public void onResponse(Call&lt;MonthlyOrderHistory&gt; call, Response&lt;MonthlyOrderHistory&gt; response) {
  
              MonthlyOrderHistory monthlyOrderHistory = response.body();
              if(monthlyOrderHistory.getStatus().equalsIgnoreCase("success")) {
  
                  List&lt;UserOrder&gt; userOrders = monthlyOrderHistory.getOrders().getUserOrder();
                  if(userOrders != null &amp;&amp; userOrders.size() != 0) {
                      DefaultScheduler.INSTANCE.execute(() -&gt; {
                          monthlyOrderHistoryMutableLiveData.postValue(monthlyOrderHistory);
                          return Unit.INSTANCE;
                      });
                  }
              }
          }
  
          @Override
          public void onFailure(Call&lt;MonthlyOrderHistory&gt; call, Throwable t) {
  
              t.printStackTrace();
          }
      });
  }
  
  }
  </code></pre>
  
  <p><code>DefaultScheduler</code> is a <code>Scheduler</code> that by default handles operations in the <code>AsyncScheduler</code>.
  Here's the Source Code of my <strong>Scheduler.kt</strong> class</p>
  
  <pre><code>
  interface Scheduler {
  
      fun execute(task: () -&gt; Unit)
  
      fun postToMainThread(task: () -&gt; Unit)
  
      fun postDelayedToMainThread(delay: Long, task: () -&gt; Unit)
  }
  
  //A shim [Scheduler] that by default handles operations in the [AsyncScheduler].
  object DefaultScheduler : Scheduler {
  
      private var delegate: Scheduler = AsyncScheduler
  
      //Sets the new delegate scheduler, null to revert to the default async one.
      fun setDelegate(newDelegate: Scheduler?) {
          delegate = newDelegate ?: AsyncScheduler
      }
  
      override fun execute(task: () -&gt; Unit) {
          delegate.execute(task)
      }
  
      override fun postToMainThread(task: () -&gt; Unit) {
          delegate.postToMainThread(task)
      }
  
      override fun postDelayedToMainThread(delay: Long, task: () -&gt; Unit) {
          delegate.postDelayedToMainThread(delay, task)
      }
  }
  
  //Runs tasks in a [ExecutorService] with a fixed thread of pools
  internal object AsyncScheduler : Scheduler {
  
      private val executorService: ExecutorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS)
  
      override fun execute(task: () -&gt; Unit) {
          executorService.execute(task)
      }
  
      override fun postToMainThread(task: () -&gt; Unit) {
          if (isMainThread()) {
              task()
          } else {
              val mainThreadHandler = Handler(Looper.getMainLooper())
              mainThreadHandler.post(task)
          }
      }
  
      private fun isMainThread(): Boolean {
          return Looper.getMainLooper().thread === Thread.currentThread()
      }
  
      override fun postDelayedToMainThread(delay: Long, task: () -&gt; Unit) {
          val mainThreadHandler = Handler(Looper.getMainLooper())
          mainThreadHandler.postDelayed(task, delay)
      }
  }
  
  //Runs tasks synchronously.
  object SyncScheduler : Scheduler {
      private val postDelayedTasks = mutableListOf&lt;() -&gt; Unit&gt;()
  
      override fun execute(task: () -&gt; Unit) {
          task()
      }
      override fun postToMainThread(task: () -&gt; Unit) {
          task()
      }
      override fun postDelayedToMainThread(delay: Long, task: () -&gt; Unit) {
          postDelayedTasks.add(task)
      }
      fun runAllScheduledPostDelayedTasks() {
          val tasks = postDelayedTasks.toList()
          clearScheduledPostdelayedTasks()
          for (task in tasks) {
              task()
          }
      }
      fun clearScheduledPostdelayedTasks() {
          postDelayedTasks.clear()
      }
  }
  </code></pre>
  
  <p>And finally, I'm trying to observe the changes from my fragment's <code>onCreateView</code> method using this:</p>
  
  <pre><code>MonthlyOrderViewModel monthlyOrderViewModel = new MonthlyOrderViewModel(getActivity().getApplication());
  
  monthlyOrderViewModel.getMonthlyOrderHistoryMutableLiveData().observe(this, monthlyOrderHistory -&gt; {
  
      monthlyOrderRecyclerView = view.findViewById(R.id.monthlyOrderRecyclerView);
  
      monthlyOrderRecyclerViewAdapter = new OrderHistoryRecyclerViewAdapter(getContext(), monthlyOrderHistory.getOrders().getUserOrder());
  
      monthlyOrderRecyclerViewAdapter.notifyDataSetChanged();
  
      monthlyOrderRecyclerView.setAdapter(monthlyOrderRecyclerViewAdapter);       
  
      monthlyOrderRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  
  });
  </code></pre>
  
  <p>I tried to debug it and found that the retrofit had no problem fetching data. But the <code>observe</code> method is never getting called when I try to use them in my fragment.</p>
  
  <p>Please suggest me how to solve this problem? Any help would be appreciated.</p>