user6761472 user6761472 - 3 months ago 9
Android Question

How to add Fragment into Activity in Android

I want add 2

fragment
into
activity
, and when click on button switch between this
fragments
!

I want show
Fragment_1
for by default in
activity
(my mean is : when running
activity
, show
fragment_1
) and when click on button switch between
fragment_2
and
fragment_1
!


I write below codes, and when running
activity
show
fragment_1
but when click on button for switch between
fragments
, show me Force Close error.

FC error :

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)


Activity codes:

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;
}

}
});
}
}


How can I fix it ? Thanks all <3

Answer

Try this 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) {
                LoginFragment f1 = new LoginFragment();
                fM.beginTransaction().add(R.id.login_cardView, f1)
                        .commit();
                status = true;
            }else {
                RegisterFragment f2 = new RegisterFragment();
                fM.beginTransaction().add(R.id.login_cardView, f2)
                        .commit();
                status = false;
            }
        }
    });
}
Comments