[英]Replace Current fragment with another fragment from within the Current fragment
I have an activity named as我有一个名为的活动
MainActivity
i have added a fragment " BenefitFragment
" in container R.id.mainContainer
.我在容器
R.id.mainContainer
添加了一个片段“ BenefitFragment
”。
In BenefitFragment layout i have another container R.id.benefitContainer
.在 BenefitFragment 布局中,我有另一个容器
R.id.benefitContainer
。 I am adding a nested fragment as我正在添加一个嵌套片段作为
getChildFragmentManager().beginTransaction()
.add(R.id.benefitContainer, new ConfirmPinFragment())
.setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
.addToBackStack("benefit")
.commit();
In my ConfirmPinFragment
after some processing i want to remove this ConfirmPinFragment and replace it with another Fragment let's say TestFragment
and TestFragment can replace itself with another fragment and so on.在经过一些处理后,在我的
ConfirmPinFragment
,我想删除这个 ConfirmPinFragment 并将其替换为另一个 Fragment 假设TestFragment
和 TestFragment 可以用另一个片段替换自身等等。
Here is what i tried in ConfirmPinFragment
这是我在
ConfirmPinFragment
尝试过的
CorporateFragment fragment = new CorporateFragment();
getFragmentManager().beginTransaction()
.addToBackStack("benefit")
.hide(ConfirmPinFragment.this)
.add(android.R.id.content, fragment)
.setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
.commit();
However i am getting error android.R.id.content not found
.但是我收到错误
android.R.id.content not found
。 I want to replace the fragment from inside it and replace with another fragment, how will i do it.我想从里面替换片段并用另一个片段替换,我该怎么做。
I see BenefitFragment
will be your parent Fragment
for both ConfirmPinFragment
and CorporateFragment
.我看到
BenefitFragment
将成为ConfirmPinFragment
和CorporateFragment
父Fragment
。
So pass R.id.benefitContainer
as a container of your fragment in which your fragment to be replaced.因此,将
R.id.benefitContainer
作为要替换的片段的容器传递。 But you need to resolve that id first.但是您需要先解决该 ID。 to do so you need to use
getParentFragment()
.为此,您需要使用
getParentFragment()
。
Try to write this in your ConfirmPinFragment
.尝试在您的
ConfirmPinFragment
写入此ConfirmPinFragment
。
Fragment mF = getParentFragment();
// double check
if (mF instanceof BenefitFragment) {
getFragmentManager().beginTransaction()
.add(((BenefitFragment)getParentFragment()).getView().findViewById(R.id.benefitContainer).getId()
, new CorporateFragment())
.setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
.addToBackStack("benefit")
.commit();
}
As per your requirement, BenefitFragment is your parent fragment and you added ConfirmPinFragment successfully as it's child fragment.根据您的要求,BenefitFragment 是您的父片段,并且您成功添加了 ConfirmPinFragment 作为它的子片段。 Now other fragments let's say TestFragment Or CorporateFragment etc are child fragment of BenefitFragment same as of ConfirmPinFragment.
现在其他片段让我们说 TestFragment 或 CorporateFragment 等是 BenefitFragment 的子片段,与 ConfirmPinFragment 相同。 So you have to do same as you had done with ConfirmPinFragment adding code.
所以你必须像使用 ConfirmPinFragment 添加代码那样做。 Just replace add method with the replace .
只需更换与替换add方法。
getChildFragmentManager().beginTransaction()
.replace(R.id.benefitContainer, new CorporateFragment())
.setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
.addToBackStack("ConfirmPin")
.commit();
And your layout container for all child fragment will be R.id.benefitContainer
所有子片段的布局容器将是
R.id.benefitContainer
try this code:试试这个代码:
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, tag).addToBackStack(tag).commit();
//"content_frame" is the name of your view where the Fragment will be shown and //"fragment" is the name of your Fragment
the above code will replace your Fragment上面的代码将替换您的 Fragment
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.