【问题标题】:Recursive entry to executePendingTransactionexecutePendingTransaction 的递归入口
【发布时间】:2013-09-03 18:48:59
【问题描述】:

简而言之,我有这样的结构:

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

我得到了illegal state exception:recursive entry to executePendingTransaction。我知道如何解决它,但我只是好奇为什么会发生这种情况。在我看来是这样的:

  1. 在活动的 onCreate 中,我创建了片段并将其传递给 寻呼机的适配器。
  2. FragmentManager 调用片段的 onCreateView

  3. 我调用了 onViewCreated 并将我的片段传递给寻呼机的适配器 再次,我想它会再次进入第 2 步。

【问题讨论】:

  • 您的代码没有多大意义。你有一个带有片段的ViewPager,当ViewPager在他们的onCreateView()方法中构建这些片段时,你调用一个回调,在同一个ViewPager上设置一个新的适配器,适配器将包含ViewPager的片段已经在添加它们的过程中。另外,请不要在你的问题标题前加上Android,底部的标签就足够了。
  • 我知道,这有点伪代码:这就是我最后得到的,我的意思是调用层次结构。那我说得对吗? FragmentManager->创建Fragment视图->因为在同一个ViewPager上设置了新的adapter,FragmentManager试图再次添加这个fragment(它现在正在添加它,所以我们得到了递归)
  • 类似的事情,你正在尝试使用已经参与事务的片段进行事务。
  • 非常感谢!你真的应该把它作为一个答案,所以我可以把它标记为有用。

标签: android recursion android-fragments fragmentmanager


【解决方案1】:

但我只是好奇为什么会这样。

您没有正确使用片段。现在,您首先构建 ViewPager 及其包含片段的适配器。问题在于,在这些片段中,您有一个回调,它会触发使用相同片段在ViewPager 上设置新适配器。由于您已经在流程中进行了交易,因此尝试在该流程中进行另一笔交易将不起作用。

【讨论】:

    猜你喜欢
    • 2014-04-04
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2013-04-12
    相关资源
    最近更新 更多