[英]No view found for id when popping backstack without adding to backstack
I am working on an Android app and have encountered an error involving Fragments and FragmentTransactions. 我正在开发一个Android应用程序,遇到了涉及Fragments和FragmentTransactions的错误。 I have created an example app to demonstrate the problem.
我创建了一个示例应用程序来演示此问题。 Here is what I am doing:
这是我在做的事情:
add
Fragment1
to fragmentSpace
, without adding to backstack. add
Fragment1
add
到fragmentSpace
,而不添加到backstack。 replace
fragmentSpace
with Fragment2
, adding to backstack as "beginning"
. Fragment2
replace
fragmentSpace
,将backstack添加为"beginning"
。 add
Fragment3
to fragmentSpace2
(which is inside of Fragment2
), without adding to backstack. add
Fragment3
add
到fragmentSpace2
(位于Fragment2
内部),而不添加到backstack。 replace
fragmentSpace2
with Fragment4
, adding to backstack as null
. Fragment4
replace
fragmentSpace2
,将backstack添加为null
。 getFragmentManager().popBackStack("beginning", FragmentManager.POP_BACK_STACK_INCLUSIVE);
getFragmentManager().popBackStack("beginning", FragmentManager.POP_BACK_STACK_INCLUSIVE);
in an attempt to undo all the Transactions, which should bring the user back to seeing only Fragment1
. Fragment1
。 However, the app crashes on step 5 with this error: 但是,应用程序在第5步崩溃时出现此错误:
java.lang.IllegalArgumentException: No view found for id 0x7f090004 (me.MitchT.testfragmenttransactions:id/fragmentSpace2) for fragment Fragment3{7f35cb6 #2 id=0x7f090004}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:1645)
...
I have found that if I add step 3 to the backstack, step 5 no longer crashes the app and there is no more error. 我发现如果我将第3步添加到backstack,第5步不再崩溃应用程序,并且没有更多错误。 Why?
为什么?
The problem I have with adding step 3 to the backstack is that I want the user to be able to press the back button after Fragment3
is added and go back to Fragment1
. 我在向Backstack添加第3步时遇到的问题是我希望用户能够在添加
Fragment3
之后按下后退按钮并返回Fragment1
。 If I add it to the backstack, then when the user presses the back button it removes Fragment3
, but Fragment2
is still visible, so they must press the back button again . 如果我将它添加到backstack,那么当用户按下后退按钮时它会删除
Fragment3
,但Fragment2
仍然可见,所以他们必须再次按下后退按钮。 I am trying to avoid that double-back-button behavior. 我试图避免这种双后退按钮的行为。
I did not feel it was necessary to post the code for every single file in the example app on this question, so instead I created a gist. 我觉得没有必要在这个问题的示例应用程序中发布每个文件的代码,所以我创建了一个要点。
So I guess my questions are: 所以我想我的问题是:
Thanks! 谢谢!
From what i understand. 据我所知。 You have a fragment layout which is the container in activity
您有一个片段布局,它是活动中的容器
Activity hosts Fragment1. 活动主持片段1。 Then you replace Fragment 1 by Fragment 2 ( added to back stack.
然后用Fragment 2替换Fragment 1(添加到后栈。
At this stage clicking back button you will have fragment2 popped and you see the activity which hosts fragment1. 在此阶段单击后退按钮,您将弹出fragment2,并且您将看到托管fragment1的活动。
Fragment2 has a framelayout which holds fragment3. Fragment2有一个framelayout,它包含fragment3。 Then you have Fragment4 replacing fragment3 which is added to back stack.
然后你有Fragment4替换fragment3,它被添加到后栈。
Now clicking the button you have getFragmentManager().popBackStack("beginning", FragmentManager.POP_BACK_STACK_INCLUSIVE);
现在点击你有
getFragmentManager().popBackStack("beginning", FragmentManager.POP_BACK_STACK_INCLUSIVE);
的按钮getFragmentManager().popBackStack("beginning", FragmentManager.POP_BACK_STACK_INCLUSIVE);
. 。 But there is no view with that id.
但是没有关于那个id的观点。
Just use popBackStack()
. 只需使用
popBackStack()
。 Fragment4 --> Fragment2 ( hosting fragment3) --> Fragment1 ( hosted by the activity). Fragment4 - > Fragment2(托管片段3) - > Fragment1(由活动托管)。
If you want Fragment4 to Activity hosting Fragment1 don't add Fragment4 to the back stack. 如果你想Fragment4到Activity托管Fragment1,不要将Fragment4添加到后台堆栈。
Note : In case you want nested fragments you need to use getChildFragmentManager()
. 注意:如果您需要嵌套片段,则需要使用
getChildFragmentManager()
。 https://developer.android.com/about/versions/android-4.2.html https://developer.android.com/about/versions/android-4.2.html
Also note the discussion in the comments sections and the link posted by op Is this the right way to clean-up Fragment back stack when leaving a deeply nested stack? 还要注意注释部分中的讨论以及op发布的链接这是否是在离开深层嵌套堆栈时清理Fragment后端堆栈的正确方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.