putFragment() - 片段x不是当前在FragmentManager [英] putFragment() - Fragment x is not currently in the FragmentManager

查看:491
本文介绍了putFragment() - 片段x不是当前在FragmentManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上面的标题已经被问了很多,但答案似乎紧密联系在一起的 FragmentStatePagerAdapter 这无关我的问题。我使用 putFragment(束,弦乐,片段)直接法。

The above title has been asked a lot, but answers seem closely tied to FragmentStatePagerAdapter which has nothing to do with my problem. I'm using the method putFragment(Bundle, String, Fragment) directly.

的<一个href=\"http://developer.android.com/reference/android/app/FragmentManager.html#putFragment(android.os.Bundle,%20java.lang.String,%20android.app.Fragment)\"相对=nofollow>为 putFragment(束,弦乐,片段)Android文档 说:

The Android Documentation for putFragment(Bundle, String, Fragment) says:

Put a reference to a fragment in a Bundle. This Bundle can be persisted as saved state, and when later restoring getFragment(Bundle, String) will return the current instance of the same fragment.

Parameters
* bundle        The bundle in which to put the fragment reference.
* key           The name of the entry in the bundle.
* fragment  The Fragment whose reference is to be stored.

但是以下code抛出异常:

However the following code throws an exception:

Bundle bundle = new Bundle();
CustomFragment actionBarFragment = getActionBarFragment();
CustomFragment contentFragment   = getContentFragment();
actionBarFragment.setArguments(bundle);
contentFragment.setArguments(bundle);

FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
mTransaction.add(R.id.actionBarPane, actionBarFragment);
mTransaction.add(R.id.contentPane,   contentFragment);
mTransaction.commit();

getSupportFragmentManager().putFragment(bundle, "ContentFragment", contentFragment);
getSupportFragmentManager().putFragment(bundle, "ActionBar", actionBarFragment);

我使用上述的原因是这样既ContentFragment和动作条片段可以使用 getArguments的结果()来找到自己的对手,即使他们AREN 'T目前在backstack的顶部 - 例如,如果他们被部分透明的碎片在堆栈中较高的遮挡

The reason I'm using the above is so that both ContentFragment and ActionBar fragment can use the result of getArguments() to find their opposite number, even if they aren't currently in the top of the backstack - such as if they are partially occluded by transparent Fragments higher in the stack.

不过,我得到异常:

11-20 13:44:17.842: E/Default Exception Handler(12466): Caused by: java.lang.IllegalStateException: Fragment CustomFragment{4219bdb8 id=0x7f06002e com.test.CustomFragment1} is not currently in the FragmentManager

我可以从这个结论是提交()仅仅把堆栈上的交易要在UI线程和 putFragment上完成() 呼叫发生的交易正在开展过吗?还是我误解的东西? (在Android网站上的文件并没有说明prerequisite状态什么的片段是在我以为它应该)。

Can I conclude from this that commit() simply puts the transaction on a stack to be done on the UI thread and the putFragment() calls are happening before that transaction is being carried out? Or am I misunderstanding something? (The documentation on the Android site doesn't say anything about prerequisite states for the Fragments to be in which I assume it should).

值得注意的文本提交(这就是为什么我认为呼叫发生得太早 - 一个可能的答案是如何侦听器连接到一个交易通知您,当它已经提交()编辑。我只是不认为存在...

It's worth noting the text in commit() which is why I assume the call is happening too early - A possible answer being how to attach a listener to a transaction to notify you when it has been commit()ed. I just don't think that exists...

时间表提交本次交易。提交不会立即发生;它将被安排为在主线程工作下一次线程准备工作要做。

Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.

修改

证实提交()是使用一个可怕的解决这个问题:

Confirmed that commit() is the problem by using a terrible solution:

mTransaction.commit();
new Thread() {
    public void run() {
        while(!contentFragment.isAdded()) {
            try {
                Thread.sleep(100);
            } catch (Exception e) {}
        }
        getSupportFragmentManager().putFragment(bundle, "ContentFragment", contentFragment);
        getSupportFragmentManager().putFragment(bundle, "ActionBar", actionBarFragment);
    };
}.start();

解决方案实时仍然非常AP preciated。

Real solutions still very much appreciated.

推荐答案

我从来没有用它自己,但你看了<一个href=\"http://developer.android.com/reference/android/app/FragmentManager.html#executePendingTransactions()\"相对=nofollow> FragmentManager.executePendingTransactions()?这里的文件说什么:

I've never used it myself, but have you looked at FragmentManager.executePendingTransactions()? Here's what the documentation says:

在一个FragmentTransaction致力于与
  FragmentTransaction.commit(),它计划将被执行
  在异步进程的主线程。如果你想
  立即执行任何此类未决操作,您可以调用这个
  功能(仅由主线程)这样做。请注意,所有回调
  和其他相关行为将从此调用中完成,所以
  小心,这哪里从中调用。

After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process's main thread. If you want to immediately executing any such pending operations, you can call this function (only from the main thread) to do so. Note that all callbacks and other related behavior will be done from within this call, so be careful about where this is called from.

这听起来像是你的使用情况相符。

It sounds like it matches your use case.

这篇关于putFragment() - 片段x不是当前在FragmentManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆