如何从另一个片段中打开一个新片段? [英] How do I open a new fragment from another fragment?
本文介绍了如何从另一个片段中打开一个新片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在片段之间进行导航.我有NewFragment.java
,并且新的片段正在工作.我的问题是:
I tried making a navigation between fragments. I've got the NewFragment.java
with the new fragment working. My problem is:
如何使此onClickListener
正确运行NewFragment.java
?
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), NewFragment.class);
startActivity(i);
}
});
仅供参考:这是从一个片段中提取的(我不知道这是否重要).
FYI: This is from inside a fragment (I don't know if that matters).
推荐答案
在点击侦听器函数中添加以下代码,
Add following code in your click listener function,
NextFragment nextFrag= new NextFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.Layout_container, nextFrag, "findThisFragment")
.addToBackStack(null)
.commit();
如果需要,可以使用字符串"findThisFragment"
稍后查找片段.
The string "findThisFragment"
can be used to find the fragment later, if you need.
这篇关于如何从另一个片段中打开一个新片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文