Android FragmentTransaction 何时提交?

发布于 2024-12-01 21:54:50 字数 1122 浏览 6 评论 0原文

我正在构建一个平板电脑应用程序。在此应用程序中,有一个具有两个片段的活动。第一个片段是“已知”列表片段,它显示来自数据库查询的简单单项布局列表,第二个片段显示有关所选记录的详细信息(来自列表片段)。第二个片段的想法是它的类型取决于列表中显示的记录。例如,如果记录是客户,则显示所选客户的详细信息,如果记录是库存项目,则显示所选项目的详细信息等。 为了与详细信息片段进行通信,我创建了一个每个详细信息片段类都实现的接口。 列表片段在布局 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 始终为 null。这是因为fragmentTransaction.commit()不会立即运行,而是在下次线程准备好时在主线程上发生(如android文档所述)。

如果我在 onStart() 中创建详细信息片段并在 onCreate 中实例化列表片段,则一切正常。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皇甫轩 2024-12-08 21:54:50

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

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

愛放△進行李 2024-12-08 21:54:50

在 Android API 24 FragmentTransaction 中,具有同步 .commitNow() 方法。
它现在在参考中: https://developer.android.com /reference/android/app/FragmentTransaction.html#commitNow()

相反,.commit() 异步工作。它只是安排事务的提交。

In Android API 24 FragmentTransaction has synchronous .commitNow() method.
It's in the reference now: https://developer.android.com/reference/android/app/FragmentTransaction.html#commitNow()

On the contrary, .commit() works asynchronously. It just schedules a commit of the transaction.

衣神在巴黎 2024-12-08 21:54:50

我面临着类似的问题。

我认为这里的关键学习是使用 commitNow() 而不是 commit() 和 getSupportFragmentManager。这将不允许主线程执行,直到片段被销毁。在构建界面和使用共享活动时,这是必不可少的。我应该知道它让我难住了一段时间!

这是一个示例代码示例:
getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.fragment_frame)).commitNow();

I was facing a similar issue.

I think the key learning here is using commitNow() instead of commit() with getSupportFragmentManager. This will disallow the main thread from executing until the fragment has been destroyed. It is imperative when building interfaces and using a shared activity. I should know it had me stumped for a while!

Here is a sample code example:
getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.fragment_frame)).commitNow();

雄赳赳气昂昂 2024-12-08 21:54:50

“...所以我可以使用添加的片段做一些工作?此外,有什么方法可以等到提交发生,然后然后继续处理其余代码 ?”

这完全取决于您想要做什么工作。从您的问题中我看到您的大部分工作代码无论如何都应该在您的片段代码中,例如当选择库存项目时。

在回调中,当选择一个选择列表项(为了更改详细信息片段)时,您将能够足够轻松地获取详细信息片段。

此外,您已经从 FragmentsPool.getHelperFileFragment(501), 返回中获得了片段,因此我不明白为什么您需要通过其标签获取片段。

我很想知道您需要在 onCreate 中使用添加的详细信息片段进行哪些工作。

"....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?"

It all depends on what work you want to do. From your question I see that most of your work code should be in your fragment code anyway, for example when an inventory item is selected.

On the callback when a selection list item is selected (in order to change the details fragment) you'll be able to get hold of the details fragment comfortably enough anyway.

Furthermore, you already have the fragment from the return of FragmentsPool.getHelperFileFragment(501), so I don't see why you need to get the fragment via its tag.

I'm interested to know what work you need to do in onCreate with your added details fragment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文