为什么 Fragments 需要交易?

Why do Fragments require a transaction?

我想知道为什么与 Activity 不同,Fragment 要求我们将其放入交易中。跟转场动画有关系吗?

A​​ 事务组 all UI 更改,以便它们由 FragmentManager 同时执行(同时也可能进行性能优化) .与一次处理整个屏幕的 activity 不同,这通常涉及同时更新屏幕的各个部分。

当然,这也可以手动实现,但是事务的概念使实现变得 容易。

引自documentation

Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, you must call commit().