简体   繁体   中英

This transaction should be completed with a commit call

I have this code to change fragment:

FragmentTransaction fragmentTransaction = getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.frame_content, fragment);

if (isBackable) fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

Android studio warn:

This transaction should be completed with a commit() call

I don't know how the correct way to solve this warning.

It's a false positive.

Android Lint only sees you have chained some fragment transaction calls together without a commit but fails to see the commit on a later row.

You can either

  1. ignore the warning,

  2. suppress it with @SuppressLint("CommitTransaction") , or

  3. remove the method chaining ie replace

     FragmentTransaction fragmentTransaction = getSupportFragmentManager() .beginTransaction() .replace(R.id.frame_content, fragment); 

    with

     FragmentTransaction fragmentTransaction = getSupportFragmentManager() .beginTransaction(); fragmentTransaction.replace(R.id.frame_content, fragment); 

i think if you try this the warn will dispear

FragmentTransaction fragmentTransaction = getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.frame_content, fragment);

if (isBackable) {
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

or

  FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.frame_content, fragment);

    if (isBackable) fragmentTransaction.addToBackStack(null).commit();

or

you could just suppress the warning

good luck !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM