FragmentManager和FragmentTransaction到底做什么? [英] What does FragmentManager and FragmentTransaction exactly do?

查看:419
本文介绍了FragmentManager和FragmentTransaction到底做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有简单的代码

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

这些代码行是做什么的?

What do these lines of code do?

推荐答案

getFragmentManager()

返回FragmentManager以与关联的片段进行交互 进行此活动.

Return the FragmentManager for interacting with fragments associated with this activity.

FragmentManager,用于创建用于添加,删除或替换片段的事务.

FragmentManager which is used to create transactions for adding, removing or replacing fragments.

fragmentManager.beginTransaction();

对与 这个FragmentManager.

Start a series of edit operations on the Fragments associated with this FragmentManager.

将要使用的FragmentTransaction对象.

The FragmentTransaction object which will be used.

fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);

将当前片段替换为ID为R.id.fragment_container

Replaces the current fragment with the mFeedFragment on the layout with the id: R.id.fragment_container

fragmentTransaction.addToBackStack(null);

将此事务添加到后台堆栈.这意味着 事务在提交后将被记住,并且将被撤消 稍后从堆栈中弹出时其操作.

Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

对于返回按钮的用法很有用,因此可以回滚事务. 参数名称:

Useful for the return button usage so the transaction can be rolled back. The parameter name:

是此堆叠状态的可选名称,或者为null.

Is an optional name for this back stack state, or null.

有关其他问题的信息,请参见使用null参数的addToBackStack吗?

See for information the other question What is the meaning of addToBackStack with null parameter?

Last语句将提交事务并执行所有命令.

The Last statement commits the transaction and executes all commands.

请参阅Google文档以获取更多帮助:

See the google documentation for more help:

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html http://developer.android.com/reference/android/app/FragmentManager.html http://developer.android.com/reference/android/app/FragmentTransaction.html

这篇关于FragmentManager和FragmentTransaction到底做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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