Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This transaction should be completed with a commit call

Tags:

java

android

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.

like image 505
nafsaka Avatar asked Mar 14 '23 23:03

nafsaka


1 Answers

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 i.e. replace

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

    with

    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
    fragmentTransaction.replace(R.id.frame_content, fragment);
    
like image 183
laalto Avatar answered Mar 25 '23 02:03

laalto