0

In brief i have such construction:

class AlbumPickerFragment extends PageFragment {

...

    @Override
    public void onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         mCallback.onViewCreated(this);
    }


}


public class PlaylistPickerActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        FragmentCreatedCallback callback = new FragmentCreatedCallback();
        if (savedInstanceState == null) {
            mFragments.add(PageFragment.newInstance(PageFragment.ALBUM_FRAGMENT_TYPE, callback));
            mAdapter = new PagerAdapter(getSupportFragmentManager(), mFragments);
            mPager.setAdapter(mAdapter);
        } else {
            restoring = true;
        }
    }


    Callback extends ICallback {

           public void onViewCreated(final Fragment fragment) {
                  mFragments.add(fragment);
                  mAdapter = new PagerAdapter(getSupportFragmentManager(), mFragments);
                  mPager.setAdapter(mAdapter);
           }
    }

I got illegal state exception: recursive entry to executePendingTransaction. I know how to fix it, but i am just curious why does this happen. In my opinion it's something like this:

  1. In activity's onCreate i created fragment and passed it to pager's adapter.
  2. FragmentManager calls fragment's onCreateView

  3. I called onViewCreated and passed my fragment to pager's adapter again AND i suppose there it goes to step 2 again.

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
Semyon Danilov
  • 1,753
  • 1
  • 17
  • 37
  • Your code doesn't make much sense. You have a a `ViewPager` with fragments, when the `ViewPager` builds those fragments in their `onCreateView()` method you call a callback which sets a new adapter on the same `ViewPager`, adapter which will contain the fragments that the `ViewPager` is already in the process of adding them. Also, please don't prefix your questions titles with Android, the tag at the bottom is more than enough. – user Sep 03 '13 at 19:03
  • I know, it's a bit of pseudo-code: that's what i got in the end, i mean Call Hierarchy. So am i right? FragmentManager->creates Fragment view->because of setting new adapter on the same ViewPager, FragmentManager trying to add this fragment again (and it is adding it right now, so we got recursion) – Semyon Danilov Sep 03 '13 at 19:14
  • 2
    Something like that, you're trying to do a transaction with a fragment which is already involved in a transaction. – user Sep 04 '13 at 05:17
  • Many thanks! You really should make it an answer, so i can mark it as useful. – Semyon Danilov Sep 04 '13 at 12:31

1 Answers1

2

but i am just curious why does this happen.

You're not using the fragments properly. Right now you first build the ViewPager along with its adapter containing fragments. The problem is that in those fragments you have a callback which triggers a new adapter to be set on the ViewPager using the same fragments. As you already have a transaction in the process, trying to make another one in the process will not work.

user
  • 86,916
  • 18
  • 197
  • 190