title: Android fragment replace only works in onCreate
body: <p>I have been searching/struggling with this for two days now and I really hope it's nothing silly. I am trying to replace a fragment. The two fragments are being swapped in the same container in the activity. The fragments <strong>BOTH</strong> load fine individually if I swap the code in the OnCreate(), but when I try to replace the fragment from a callback I get the error. It can't seem to resolve the container 'R.id.authparent'. However, in the onCreate() it resolves just fine.</p>
  
  <p>Here are the deatails:
  I have my activity. This is where the ViewModel calls back to onRegisterNewUser() gets called and the fragment should get replaced.</p>
  
  <p><strong>Activiy Class</strong></p>
  
  <pre><code>public class AuthenticationActivity extends AppCompatActivity implements IAuthenticationActivity {
  FragmentManager mgr;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_authentication);
      Fragment loginFragment = new LoginFragment();
      mgr = getSupportFragmentManager();
      FragmentTransaction trans = mgr.beginTransaction();
      trans.add(R.id.authparent, loginFragment, Constants.LOGIN_FRAGMENT);
      trans.commit();
  }
  
  @Override
  public void onRegisterNewUser() {
      Fragment fragment = new RegisterFragment();
      FragmentTransaction trans = mgr.beginTransaction();
      trans.replace(R.id.authparent, fragment);
      trans.addToBackStack(null);
      trans.commit();
  }
  }
  </code></pre>
  
  <p><strong>Login Fragment</strong></p>
  
  <pre><code>public class LoginFragment extends Fragment {
  
  private static final String TAG = "LoginFragment";
  private LoginViewModel viewModel;
  @Bind(R.id.input_email)
  EditText _emailText;
  @Bind(R.id.input_password) EditText _passwordText;
  @Bind(R.id.btn_login)
  Button _loginButton;
  @Bind(R.id.link_signup)
  TextView _signupLink;
  @Bind(R.id.login_progress)
  ProgressBar _progressBar;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_login, container, false);
      ButterKnife.bind(view);
  
      viewModel = new LoginViewModel(getContext());
      FragmentLoginBinding binding = DataBindingUtil.setContentView(getActivity(), R.layout.fragment_login);
      binding.setViewModel(viewModel);
      return view;
  }
  }
  </code></pre>
  
  <p><strong>Register Fragment</strong></p>
  
  <pre><code>public class RegisterFragment extends Fragment implements IRegisterFragment {
  private OAuthToken oauthToken = null;
  private static final String TAG = "RegisterFragment";
  private RegisterViewModel viewModel;
  @Bind(R.id.input_email)
  EditText _emailText;
  @Bind(R.id.input_username)
  EditText _usernameText;
  @Bind(R.id.input_password)
  EditText _passwordText;
  @Bind(R.id.input_verify_password)
  EditText _verifyPasswordText;
  @Bind(R.id.btn_register)
  Button _registerButton;
  @Bind(R.id.register_progress)
  ProgressBar _progressBar;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_register, container, false);
      ButterKnife.bind(view);
      viewModel = new RegisterViewModel(getContext());
      FragmentRegisterBinding binding = DataBindingUtil.setContentView(getActivity(), R.layout.fragment_register);
      binding.setViewModel(viewModel);
      return view;
  }
  }
  </code></pre>
  
  <p><strong>Login ViewModel</strong> This is partial to save space, the only thing I'm including is the callback back to the activity</p>
  
  <pre><code>public class LoginViewModel extends ViewModelBase implements ILoginCallBack     {
  ...
  
  public void onSignupLink(View view) {
      if (context instanceof IAuthenticationActivity) {
          ((IAuthenticationActivity) context).onRegisterNewUser();
      }
  }
  ...
  }
  </code></pre>
  
  <p>ViewModelBase is really simple
  <strong>ViewModelBase</strong></p>
  
  <pre><code>public class ViewModelBase extends BaseObservable {
  protected final Context context;
  
  protected Context getContext(){
      return context;
  }
  
  public ViewModelBase(Context context) {
      this.context = context;
  }
  
  protected boolean checkEmpty(CharSequence s) {
      if (s.toString().isEmpty()) {
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Finally the views:
  <strong>activity_authentication</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/authparent"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:keepScreenOn="true"&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>fragment_login</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
  android:keepScreenOn="true"&gt;
  &lt;data&gt;
      &lt;variable name="viewModel" type="com.savij.splitr.viewmodels.LoginViewModel" /&gt;
  &lt;/data&gt;
  &lt;ScrollView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:fitsSystemWindows="true"&gt;
  &lt;RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
      &lt;ProgressBar
          android:id="@+id/login_progress"
          android:visibility="@{viewModel.ProgressBarVisibility}"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          /&gt;
  
  &lt;LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="46dp"
      android:paddingLeft="24dp"
      android:paddingRight="24dp"
      android:layout_alignParentTop="true"&gt;
  
      &lt;ImageView android:src="@drawable/splitr_logo_crop"
          android:layout_width="wrap_content"
          android:layout_height="250dp"
          android:layout_gravity="center_horizontal" /&gt;
      &lt;!--&lt;TextView--&gt;
          &lt;!--android:layout_width="wrap_content"--&gt;
          &lt;!--android:layout_height="wrap_content"--&gt;
          &lt;!--android:text="@string/app_title"--&gt;
          &lt;!--android:textSize="@dimen/font_title"--&gt;
          &lt;!--android:layout_marginBottom="24dp"--&gt;
          &lt;!--android:layout_gravity="center_horizontal" /&gt;--&gt;
  
      &lt;!-- Email Label --&gt;
      &lt;android.support.design.widget.TextInputLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:layout_marginBottom="8dp"
          app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"&gt;
          &lt;EditText android:id="@+id/input_email"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:inputType="textEmailAddress"
              android:text="@{viewModel.Email}"
              android:onTextChanged="@{viewModel.emailChanged}"
              android:error="@{viewModel.EmailError}"
              android:hint="Email"
              android:singleLine="true"/&gt;
      &lt;/android.support.design.widget.TextInputLayout&gt;
  
      &lt;!-- Password Label --&gt;
      &lt;android.support.design.widget.TextInputLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:layout_marginBottom="8dp"
          app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"&gt;
          &lt;EditText android:id="@+id/input_password"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@{viewModel.Password}"
              android:onTextChanged="@{viewModel.passwordChanged}"
              android:error="@{viewModel.PasswordError}"
              android:inputType="textPassword"
              android:hint="Password"
              android:singleLine="true"/&gt;
      &lt;/android.support.design.widget.TextInputLayout&gt;
  
      &lt;android.support.v7.widget.AppCompatButton
          android:id="@+id/btn_login"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="24dp"
          android:layout_marginBottom="24dp"
          android:padding="12dp"
          android:text="Login"
          android:onClick="@{viewModel.onClickLogin}"
          /&gt;
  
      &lt;TextView android:id="@+id/link_signup"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="24dp"
          android:text="No account yet? Create one"
          android:onClick="@{viewModel.onSignupLink}"
          android:gravity="center"
          android:textSize="16dip"/&gt;
  
      &lt;TextView android:id="@+id/link_lost_password"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="24dp"
          android:text="Forgot password?"
          android:onClick="@{viewModel.onForgotPassword}"
          android:gravity="center"
          android:textSize="16dip"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>
  </p>
  
  <p><strong>fragment_register</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
  android:keepScreenOn="true"&gt;
  &lt;data&gt;
      &lt;variable name="viewModel" type="com.savij.splitr.viewmodels.RegisterViewModel" /&gt;
  &lt;/data&gt;
  &lt;ScrollView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:fitsSystemWindows="true"&gt;
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;ProgressBar
              android:id="@+id/register_progress"
              android:visibility="@{viewModel.ProgressBarVisibility}"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              /&gt;
  
          &lt;LinearLayout
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:paddingTop="46dp"
              android:paddingLeft="24dp"
              android:paddingRight="24dp"
              android:layout_alignParentTop="true"&gt;
  
              &lt;ImageView android:src="@drawable/splitr_logo_solo"
                  android:layout_width="wrap_content"
                  android:layout_height="100dp"
                  android:layout_gravity="center_horizontal" /&gt;
  
              &lt;!-- Email Label --&gt;
              &lt;android.support.design.widget.TextInputLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="8dp"
                  android:layout_marginBottom="8dp"
                  app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"&gt;
                  &lt;EditText android:id="@+id/input_email"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:inputType="textEmailAddress"
                      android:text="@{viewModel.Email}"
                      android:onTextChanged="@{viewModel.emailChanged}"
                      android:error="@{viewModel.EmailError}"
                      android:hint="Email"
                      android:singleLine="true"/&gt;
              &lt;/android.support.design.widget.TextInputLayout&gt;
  
              &lt;android.support.design.widget.TextInputLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="8dp"
                  android:layout_marginBottom="8dp"
                  app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"&gt;
                  &lt;EditText android:id="@+id/input_username"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:inputType="textEmailAddress"
                      android:text="@{viewModel.Username}"
                      android:onTextChanged="@{viewModel.usernameChanged}"
                      android:error="@{viewModel.UsernameError}"
                      android:hint="Username"
                      android:singleLine="true"/&gt;
              &lt;/android.support.design.widget.TextInputLayout&gt;
  
              &lt;!-- Password Label --&gt;
              &lt;android.support.design.widget.TextInputLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="8dp"
                  android:layout_marginBottom="8dp"
                  app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"&gt;
                  &lt;EditText android:id="@+id/input_password"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="@{viewModel.Password}"
                      android:onTextChanged="@{viewModel.passwordChanged}"
                      android:error="@{viewModel.PasswordError}"
                      android:inputType="textPassword"
                      android:hint="Password"
                      android:singleLine="true"/&gt;
              &lt;/android.support.design.widget.TextInputLayout&gt;
  
              &lt;android.support.design.widget.TextInputLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="8dp"
                  android:layout_marginBottom="8dp"
                  app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"&gt;
                  &lt;EditText android:id="@+id/input_verify_password"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="@{viewModel.VerifyPassword}"
                      android:onTextChanged="@{viewModel.verifyPasswordChanged}"
                      android:error="@{viewModel.VerifyPasswordError}"
                      android:inputType="textPassword"
                      android:hint="Verify Password"
                      android:singleLine="true"/&gt;
              &lt;/android.support.design.widget.TextInputLayout&gt;
  
              &lt;android.support.v7.widget.AppCompatButton
                  android:id="@+id/btn_register"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="24dp"
                  android:layout_marginBottom="24dp"
                  android:padding="12dp"
                  android:text="Login"
                  android:onClick="@{viewModel.onClickRegister}"
                  /&gt;
  
              &lt;TextView android:id="@+id/link_signup"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginBottom="24dp"
                  android:text="Already have an account?"
                  android:onClick="@{viewModel.onLoginLink}"
                  android:gravity="center"
                  android:textSize="16dip"/&gt;
  
          &lt;/LinearLayout&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;/ScrollView&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>02-29 17:23:24.340 8064-8064/com.savij.splitr E/FragmentManager: Activity state:
  02-29 17:23:24.370 8064-8064/com.savij.splitr E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.savij.splitr, PID: 8064
  java.lang.IllegalArgumentException: No view found for id 0x7f0c006c (com.savij.splitr:id/authparent) for fragment RegisterFragment{a34c76c #1 id=0x7f0c006c}
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5835)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
  </code></pre>
  
  <p>So weird that it resolves the first time, but in the callback it blows up. Any help would be greatly appreciated!
  Thanks,
  Jeff</p>