I have a Fragment
that has a FrameLayout
. This first fragment (A) loads inside its Framelayout another fragment (B). When I call getParentFragment
from inner fragment (B), I get null
. How should this method be used properly?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
getParentFragment() was introduced in API level 17 (Android 4.2). Android 4.2 introduced the idea of nested fragments (fragments containing other fragments). Calling this results in null if the fragment has a parent which is an Activity.
Have a look at this.
If you are using support library then you can use getParent(), may be you need to use getChildFragmentManager() while doing fragment transaction. See this
I faced the same issue , and fixed the issues by hosting second fragment in your parent fragment with
getChildFragmentManager()
then you wont be getting the null value ...Parent fragment
Child fragment : what i have used is a dialog
The one thing that helped is, when creating adapter use
getChildFragmentManager()
.If you are not using adapter, just use
getChildFragmentManager()
when doing transactions.setTargetFragment()
is not recommended, since it gives errors onmoveState()
of fragment(because fragments should be tied to FragmentManager)In my case, although my fragmentA was nested in fragmentB,but I still get null after call getParentFragment in FragmentA. Finally I found that I should use getChildFragmentManager rather than getFragmentManager in FragmentB.
check this What is difference between getSupportFragmentManager() and getChildFragmentManager()?