title: Android Architecture Components: After screen rotation, fragment is called twice
body: <p>I try to understand the Android ViewModel and I am facing at a strange behaviour:</p>
  
  <p>Everything is OK when I launch the app, but on each rotation the fragment ( onCreateView..., onCreate...) is called twice.</p>
  
  <p>I use a ModelView to instantiate the Fragment in the Main Activity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      String TAG="main";
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Log.i(TAG, "onCreate: mainactivity");
          Toolbar toolbar = findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          MyModelView viewModel =  ViewModelProviders.of(this).get(MyModelView.class);
  
          viewModel.getmCurrentStep().observe(this,(MyStepModel step) -&gt; {
  
              if (step != null) {
                  Log.i(TAG, "onCreate: step "+step.getmFragment());
  
              getSupportFragmentManager()
                              .beginTransaction()
                              .replace(R.id.fragmentLayout, step.createFragment())
                              .commit();
              }
          });
      }
  }    
  </code></pre>
  
  <p>The Fragment:</p>
  
  <pre><code>public class FragmentPrincipal extends Fragment {
      String TAG="main";
      public FragmentPrincipal() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
          Log.i(TAG, "onCreateView:fragment1 ");
         return inflater.inflate(R.layout.fragment_principal, container, false);
      }
  }
  </code></pre>
  
  <p>ViewModel: Nothing particular I follow tutorials on web (Thank you for all who has written them !!!)       </p>
  
  <pre><code>public class MyModelView extends ViewModel {                   
      private MutableLiveData&lt;MyStepModel&gt; mCurrentStep=new MutableLiveData&lt;&gt;();
  
      public MyModelView() {
          mCurrentStep.setValue(MyStepModel.FRAGMENT1);
      }
  
      public MutableLiveData&lt;MyStepModel&gt; getmCurrentStep() {
          return mCurrentStep;
      }
  
      //public void toFragment1(){
      //  mCurrentStep.setValue(MyStepModel.FRAGMENT1);
      //}
  }
  
  public enum  MyStepModel {
      FRAGMENT1(FragmentPrincipal.class),
      FRAGMENT2(FragmentSecond.class);
  
      public Class&lt;? extends Fragment&gt; getmFragment() {
          return mFragment;
      }
  
      private final Class&lt;? extends Fragment&gt; mFragment;
  
      MyStepModel(Class&lt;? extends Fragment&gt; fragmentClass) {
          mFragment =fragmentClass;
      }
  
      public  Fragment createFragment(){
         String TAG ="main";
         Log.i(TAG, "createFragment:mystepmodel ");
         try {
             return mFragment.newInstance();
         } catch (InstantiationException | IllegalAccessException e) {
             throw new RuntimeException(e);
         }
     }
  }
  </code></pre>
  
  <p>I'm open to any suggestion. Have I missed something? What?</p>