Android蜂窝:如何确定何时FragmentTrancaction.commit()已经完成? [英] Android Honeycomb: How to determine when FragmentTrancaction.commit() has finished?

查看:142
本文介绍了Android蜂窝:如何确定何时FragmentTrancaction.commit()已经完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的一块code的:

I had this simple piece of code:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    //...

    NetworkWorker networkFragment = NetworkWorker.createInstance(null);
    fragmentTransaction.add(networkFragment, NETWORK);

    //...

    fragmentTransaction.commit();

    networkFragment.startURLRequest("test");
}

坠毁时,我叫 getActivity()在我的 startURLRequest()程序。

当我搬到了 startURLRequest()调用在onStart()它工作得很好。

As soon as I've moved the startURLRequest() call to onStart() it worked fine.

所以我用Google搜索了一下,发现这样的:

So I googled a bit and found this:

在调用commit()并不立即执行交易。相反,它安排其到活动的UI线程(以下简称主线程)上只要线程能够这样做的运行。如果有必要,但是,你可以从你的UI线程调用executePendingTransactions()立即执行提交的事务提交()。这样做通常是没有必要,除非该交易在其他线程工作的依赖。
  [ AndDev指南]

Calling commit() does not perform the transaction immediately. Rather, it schedules it to run on the activity's UI thread (the "main" thread) as soon as the thread is able to do so. If necessary, however, you may call executePendingTransactions() from your UI thread to immediately execute transactions submitted by commit(). Doing so is usually not necessary unless the transaction is a dependency for jobs in other threads. [AndDev guide]

现在,我的问题是...是不是真的建议使用此 executePendingTransactions()或将在此举办一些陷阱?有另一种方法来确定是否交易已经完成?只是猜测,他们可能在活动的日常旁完成似乎并不令人满意。

Now, my question is... is it really advisable to use this executePendingTransactions() or will this hold some traps? Is there another way to determine if the transaction have finished? Just guessing that they might be finished in the Activity's next routine doesn't seem satisfying.

亲切的问候,
水母

Kind regards, jellyfish

推荐答案

也许你正在思考这个错误的方式。而不是调用 startURLRequest 你的片段从活动为什么不叫 startURLRequest 在片段的 onAttach 的onCreate 等方法作为你知道该片段连接到活动和getActivity这些点()会成功。

Maybe you are thinking about this the wrong way. Rather than calling startURLRequest on your fragment from the activity why not call startURLRequest in your fragment's onAttach, onCreate etc. methods as at these points you know that the fragment is attached to the activity and getActivity() will succeed.

它似乎我该片段应在何时调用 startURLRequest ,而不是活性决定。否则,从您的活动将不得不进行测试以确保片段与片段相关联。

It does seem to me that the fragment should be deciding on when to call startURLRequest and not the activity. Otherwise from your activity would have to test to ensure the fragment is associated with a fragment.

这篇关于Android蜂窝:如何确定何时FragmentTrancaction.commit()已经完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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