4

I want to make two fragment transactions in a row.

Fragment1 fragment1 = new Fragment1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment1);
transaction.addToBackStack(null);
transaction.commit();

Fragment2 fragment2 = new Fragment2();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment2);
transaction.addToBackStack(null);
transaction.commit();

However, if I do so, there will be some concurrence problem. Is there a way for me to implement a callback function so that I can start the second transaction right after the first one is finished?

Dragon warrior
  • 1,644
  • 2
  • 24
  • 37
  • `there will be some concurrence problem` -- there certainly shouldn't be. The transactions should occur in the same order in which you commit them. – Kevin Coppock Aug 26 '14 at 21:28

1 Answers1

2

You can manually flush the queue causing the pending fragment transaction to be executed. The call you're looking for is:

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    ft.replace(android.R.id.content, fragment, name).addToBackStack(null).commit();
    fm.executePendingTransactions();

By calling the fm.executePendingTransactions(); you force all pending actions to occur before continuing.

Jessie A. Morris
  • 2,267
  • 21
  • 23