我已经设置了一个带有活动的新Android项目。这是锅炉板代码:
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
有人可以解释这是做什么的吗?从我所看到的,它检查活动是否尚未初始化,然后膨胀布局。但我不明白的是beginTransaction(),ew PlaceholderFragment()和commit()
感谢。
答案 0 :(得分:1)
您使用片段事务在FrameLayout(R.id.container)中添加/替换(etc)片段,而新的PlaceholderFragment是要放入容器的片段的新实例
答案 1 :(得分:1)
//Check whether we're recreating a previously destroyed instance
if (savedInstanceState == null) {
//Execute a transaction, replacing any existing fragment with this one inside the frame.
//Getting FragmentManager object which will control fragment acvitiy
FragmentManager fm = getFragmentManager()
//Starting a FragmentTransaction which will handle transaction to this fragment activity
FragmentTransaction ft = fragmentManager.beginTransaction();
//Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.
ft.add(R.id.container, new PlaceholderFragment());
//Schedules a commit of this transaction.
ft.commit();
}
有一个很好的解释
答案 2 :(得分:0)
FragmentManager是一个有助于管理活动可能需要的片段的类。所以在这里你基本上得到它的一个实例,你正在开始一个交易。您需要一个事务实例,因为它让运行时知道在调用它时会发生一些更改。这里'add()'就是那个改变,最后你提交它来保存那个改变。
要添加的参数是需要添加片段的布局,而PlaceHolderFragment()是您需要放入的片段的名称。
答案 3 :(得分:0)
由于碎片是要走的路,取而代之的是所有重型活动,Eclipse也适应了这种导致锅炉警报的变化。 :)
启动一个片段(不能通过Intents完成)被视为一个事务,就像在数据库中一样(我猜不是一个好的例子)。
getFragmentManager() - gets the Activities FragmentManger which is responsible to initiate FragmentTransaction.
beginTransaction() - creates a new Transaction for this particular fragment job.
new PlaceholderFragment() - is an instance of the PlaceholderFragment which you can find if scroll more in the Activity.
commit - a way to commit this trasaction and bring it to effect.
有关详细信息,请参阅Android docs.。 :)
答案 4 :(得分:0)
我的朋友很简单。 用简单的编码语言:
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.tab5, new PlaceholderFragment());
fragmentTransaction.commit();
}
如果你认为它解释了一切,那么我很高兴。否则只需告诉我添加理论信息。