title: How to add Fragment into Activity in Android
body: <p>I want add 2 <code>fragment</code> into <code>activity</code>, and when click on button <strong>switch</strong> between this <code>fragments</code>! <br>
  I want show <strong><code>Fragment_1</code></strong> for by default in <code>activity</code> (<strong>my mean is :</strong> when running <code>activity</code>, show <code>fragment_1</code>) and when click on button switch between <code>fragment_2</code> and <code>fragment_1</code> !<br></p>
  
  <p>I write below codes, and when running <code>activity</code> show <code>fragment_1</code> but when click on button for switch between <code>fragments</code>, show me Force Close error.</p>
  
  <p><strong>FC error :</strong> </p>
  
  <pre><code>    08-27 14:15:41.363 14224-14224/com.mohammad.nouri.diaryday E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.mohammad.nouri.diaryday, PID: 14224
   java.lang.IllegalStateException: commit already called
       at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:641)
       at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:621)
       at com.mohammad.nouri.diaryday.Activities.LoginActivity$1.onClick(LoginActivity.java:58)
       at android.view.View.performClick(View.java:4764)
       at android.view.View$PerformClick.run(View.java:19844)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5349)
       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:908)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
  </code></pre>
  
  <p><strong>Activity codes:</strong></p>
  
  <pre><code>public class LoginActivity extends AppCompatActivity {
  
      private Context context;
      private boolean status = false;
      FragmentTransaction fT;
      FragmentManager fM;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_login);
  
          context = this;
  
          Glide.with(this).load(R.drawable.login_header_image)
                  .bitmapTransform(new BlurTransformation(context, 20))
                  .into((ImageView) findViewById(R.id.login_background));
  
          fM = getSupportFragmentManager();
          fT = fM.beginTransaction();
  
          if (!status) {
              LoginFragment f1 = new LoginFragment();
              fT.add(R.id.login_cardView, f1);
              fT.commit();
              status = true;
          }
  
          FloatingActionButton f = (FloatingActionButton) findViewById(R.id.login_registerButton);
          f.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
  
                  if (!status) {
                      RegisterFragment f1 = new RegisterFragment();
                      fT.add(R.id.login_cardView, f1);
                      fT.commit();
                      status = true;
                  }else {
                      LoginFragment f2 = new LoginFragment();
                      fT.add(R.id.login_cardView, f2);
                      fT.commit();
                      status = false;
                  }
  
              }
          });
      }
  }
  </code></pre>
  
  <p>How can I fix it ? Thanks all &lt;3</p>