title: Android: ViewModel. Failed to call observer method
body: <p>I'm using new arch. components from google.</p>
  
  <p>I have in Activity Login/Registering Fragments that are managed thanks to <code>FragmentTransaction</code> </p>
  
  <p><code>Activity-&gt;RegisterFragment (with ViewPager) -&gt; RegistrationSteps (adapter)</code></p>
  
  <p>Inside RegisterFragment I have ViewPager.
  I want that all pages inside <code>ViewPager</code> will use the same ViewModel. </p>
  
  <p>Those are registration steps (<code>RegistrationStepFragment</code>) that takes parent <code>RegistrationFragment</code> <strong>LifecycleOwner</strong> that scoped <strong>ViewModel</strong> to it- I just wanted ViewModel to be scoped to this parent Fragment. </p>
  
  <p><strong>RegistrationFragment.class</strong> inhered from</p>
  
  <pre><code>public interface FragmentViewPagerListener&lt;T extends LifecycleFragment&gt; {
      void nextPage();
      T getLifecycleFragment();
  }
  </code></pre>
  
  <p><strong>RegistartionSteps (pages) inhered from</strong></p>
  
  <pre><code>public abstract class RegisterStepFragment extends LifecycleFragment {
      protected FragmentViewPagerListener mListener;
      protected RegisterViewModel mViewModel;
  
      public void setListener(FragmentViewPagerListener fragmentViewPagerListener) {
          this.mListener = fragmentViewPagerListener;
      }
  
      protected abstract void observeViewModel();
  
      @Override
      public void onCreated(@Nullable Bundle savedInstanceState) {
          super.onCreated(savedInstanceState);
          mViewModel = ViewModelProviders.of(mListener.getLifecycleFragment()).get(RegisterViewModel.class);
          observeViewModel();
      }
  
      protected abstract boolean validateData();
  }
  </code></pre>
  
  <p>All goes well until I reach 3 page, and I want to move back (to second page) Then is thrown exception in <code>mViewPager.setCurrentItem(1)</code> (page 2: index: 1)</p>
  
  <pre><code>*java.lang.RuntimeException: Failed to call observer method
                                                                        at android.arch.lifecycle.ReflectiveGenericLifecycleObserver.invokeCallback(ReflectiveGenericLifecycleObserver.java:79)
                                                                        at android.arch.lifecycle.ReflectiveGenericLifecycleObserver.invokeMethodsForEvent(ReflectiveGenericLifecycleObserver.java:53)
                                                                        at android.arch.lifecycle.ReflectiveGenericLifecycleObserver.invokeCallbacks(ReflectiveGenericLifecycleObserver.java:61)
                                                                        at android.arch.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:45)
                                                                        at android.arch.lifecycle.LifecycleRegistry$ObserverWithState.sync(LifecycleRegistry.java:209)
                                                                        at android.arch.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:102)
                                                                        at android.arch.lifecycle.Li*fecycleDispatcher.dispatchIfLifecycleOwner(LifecycleDispatcher.java:150)
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Ok, I've found out that when moving to previous page, Fragment was reCreated calling and <code>mViewModel.observable()</code> received previously <strong><em>SUCCESS</em></strong> message and caused viewpager to move forward, that causes to destroy just created fragment what was causing error.</p>
  
  <p>Solution is to create <a href="https://github.com/googlesamples/android-architecture/blob/dev-todo-mvvm-live/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/SingleLiveEvent.java" rel="noreferrer">SingleEventLiveData</a> that emits values only when post is called (don't notify observer if value changed before observer attached)</p>
  
  <p>I mark it as CLOSE</p>