Android FragmentTransaction 何时提交? [英] Android FragmentTransaction commit When?

查看:27
本文介绍了Android FragmentTransaction 何时提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个平板电脑应用.在这个应用程序中,有一个带有两个片段的活动.第一个片段是一个已知"列表片段,它显示来自数据库查询的简单单项布局列表,第二个片段显示有关所选记录的详细信息(来自列表片段).第二个片段的想法是它的类型取决于列表中显示的记录.例如,如果记录是客户,则显示所选客户的详细信息,如果它们是库存项目,则显示所选项目的详细信息等.为了与细节片段通信,我创建了一个接口,每个细节片段类都实现了这个接口.列表片段在来自布局 xml 的活动中是固定的".然而,细节片段是在活动的创建过程中创建的,如下所示:

super.onCreate(savedInstanceState);setContentView(R.layout.act_hlpfiles_host);...FragmentManager fragmentManager = getFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.add(R.id.laydetailsfragment, FragmentsPool.getHelperFileFragment(501), "recordDetails");fragmentTransaction.commit();myDetailsFragment = getFragmentManager().findFragmentByTag("recordDetails");...myListFragment = (frg_hlpfiles_lstrecords) getFragmentManager().findFragmentById(R.id.frg_lstrecords);....}

此代码的问题在于 myDetailsFragment 始终为空.这是因为 fragmentTransaction.commit() 不会立即运行,而是在主线程下一次准备就绪时发生(如 android 文档所述).

如果我在 onStart() 中创建细节片段并在 onCreate 中实例化列表片段,一切正常.

所以问题是:我如何确定 fragmentTransaction.commit() 已提交事务,以便我可以对添加的片段进行一些工作?此外,有没有办法等到提交发生,然后继续其余的代码?

解决方案

在提交事务之后但在按标签查找之前尝试运行 fragmentManager.executePendingTransactions() 并查看是否适合您.>

I am building a tablet app. In this app there is one activity with two fragments. First fragment is a "known" list fragment which is showing a simple one item layout list from a database query, the second fragment shows the details about the selected record (from the list fragment). The think with the second fragment is that its type depends from the records being showed in the list. For example if the records are customers then the selected customer's details are shown, if they are inventory items the selected item's details are shown etc. In order to communicate with the Details Fragment I've created an interface which every detail fragment class implements. The list fragment is "fixed" in the activity from the layout xml. The detail fragment however is created during the activity's creation like this:

super.onCreate(savedInstanceState);
setContentView(R.layout.act_hlpfiles_host);

...

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.laydetailsfragment, FragmentsPool.getHelperFileFragment(501), "recordDetails");
fragmentTransaction.commit();

myDetailsFragment = getFragmentManager().findFragmentByTag("recordDetails");

...

myListFragment = (frg_hlpfiles_lstrecords) getFragmentManager().findFragmentById(R.id.frg_lstrecords);

....
}

The problem with this code is that myDetailsFragment is always null. This is because the fragmentTransaction.commit() does not run immediately but it happens on the main thread the next time that thread is ready (as the android documentation states).

If I create the detail fragment in onStart() and instantiate the list fragment in onCreate everything works ok.

So the question is: How can I be sure that the fragmentTransaction.commit() has commit the transaction so I can do some work with the added fragment? Furthermore is there any way to wait until the commit happens and then continue with the rest of the code?

解决方案

Try running fragmentManager.executePendingTransactions() after committing your transaction but before finding by tag and see if that works for you.

这篇关于Android FragmentTransaction 何时提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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