You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
@FragmentById does not work on child of fragment. #1302
In API 21+ and possibly earlier, i have only seen this when using api 21. The call getFragmentManager().getFragmentById wont get fragments that are children of a fragment, thus when using FragmentById as a child fragment of a EFragment you will get null.
when finding fragment that is a child of a fragment, getChildFragmentManager().findFragmentById()....
yeah you are right. it seems that we have to call getChildFragmentManager()... but i also think, that we might need to have an option to decide if the getFragmentManager() should be used or if the getChildFragmentManager() should be used. as for not all fragment injecting annotations use the getFragmentManager() and if we just change that behaviour this would result in a breaking change.
i think to support this we have to add a parameter @FragmentById and @FragmentByTag to let the generated code use the correct manager. i think i would call it useChildFragmentManager or something like that with a default value of false so that it keeps working for existing applications and then the user can set it to true to allow the usage of getChildFragmentManager().
Yeah, a boolean flag would be the most flexible. Although getChildFragmentManager() is only available in API level 17 and in never revision of the support lib. If it is not available in the actual API Fragment we should just invalidate, but what should happen if it is available in the API, but the app has a lower miSdkVersion?
if not in the api (due to lower target than 17): invalidate
if native fragment on minsdk < 17 but with target >= 17: invalidate (as it cant be used on every possible target/device)
if support fragment but old revision: add a note to the javadoc that this feature requires support-v7 rev >= x and let it fail on compile time if we can't (in)validate here.
I am wondering about one thing: currently EFragments uses the context Activity's FragmentManager. This is the same with Fragment.getFragmentManager() if the EFragment is directly atteched to the Activity. However, if it is nested, the two are not the same, since the second returns the parent Fragment's getChildFragmentManager(). I think it makes more sense to use Fragment.getFragmentManager(). @yDelouis@dodgex ?
I started to implement this feature, and this came into my mind: does this feature actually make sense?
I mean if you want to use getChildFragmentManager(), then you obviously used that to add the Fragments. But when you added the Fragments for example in onCreate(), then you already had the chance to store it in fields, so it is totally unnecessary to make AA inject them later. @wintersandroid what is the use case here? 😕
My use case is loading embedded nested fragments from the layout xml for a fragment. It seems like it should just fall back on getChildFragmentManager if we're on API >= 17 and the getFragmentManager wasn't able to find a fragment?
The fallback is a definitely no go, since it could be a source of bugs. We should leverage this decision to the client, with the proposed childFragment flag.
With the current AA, all of my Fragments are null if I build them in the layout for another fragment. As a really dumb work around, this fixes the issue and gives me non-null fragments when I need them:
<LinearLayout >
<RelativeLayout
<!-- cut out a bunch of pretty standard layout -->
</RelativeLayout>
<fragmentandroid:id="@+id/myFragment"android:name="com.foo.views.MyFragment_" />
</LinearLayout>
Thanks @WonderCsabo! If it helps at all, I started running into the problem of the null fragments when I set my target API to 21 from 20. I also started seeing the issue in my robolectric tests after updating to robolectric 3.0-rc3 from 2.4.
My use-case is having a fragment in a view pager, where on a landscape tablet, there is two children (master/detail) and on phone or portrait there is only the master detail..
It would be ideal to just have the fragments loaded from the XML layouts, currently I have code in the @AfterInject to create the child fragments into frame layouts on the master fragment..
So Ideally..
(this is rough hand written code, I may have the fragment xml syntax wrong)
Thanks guys! I have to be honest, i forgot that we can load the Fragments into a Fragment from XML, so those Fragments will be child Fragments. In that case, this is indeed a valid use-case. I will test it, and then continue my implementation.
@Mehly I think this got removed from the final AA because nested fragments are only supposed to be supported when the fragment is loaded dynamically. I think they happened to work ok for a while, but start having problems in later Android versions. See the PR for more details.
Activity
dodgex commentedon Jan 2, 2015
yeah you are right. it seems that we have to call
getChildFragmentManager()... but i also think, that we might need to have an option to decide if thegetFragmentManager()should be used or if thegetChildFragmentManager()should be used. as for not all fragment injecting annotations use thegetFragmentManager()and if we just change that behaviour this would result in a breaking change.i think to support this we have to add a parameter
@FragmentByIdand@FragmentByTagto let the generated code use the correct manager. i think i would call ituseChildFragmentManageror something like that with a default value offalseso that it keeps working for existing applications and then the user can set it totrueto allow the usage ofgetChildFragmentManager().@WonderCsabo @yDelouis any better ideas?
WonderCsabo commentedon Jan 3, 2015
Yeah, a boolean flag would be the most flexible. Although
getChildFragmentManager()is only available in API level 17 and in never revision of the support lib. If it is not available in the actual API Fragment we should just invalidate, but what should happen if it is available in the API, but the app has a lowermiSdkVersion?dodgex commentedon Jan 3, 2015
>= xand let it fail on compile time if we can't (in)validate here.did i forgot a case?
WonderCsabo commentedon Jan 3, 2015
I think it would be better invalidating than generating non-compilable code (we can check whether a method exists, like here).
dodgex commentedon Jan 3, 2015
yeah. where possible invalidate. but imo users should use up to date android support libs anyway
WonderCsabo commentedon Jan 4, 2015
BTW, we also have to validate that the user only set that parameter to
trueif the enclosing class is aFragment.dodgex commentedon Jan 4, 2015
yeah. i missed to mention that in my first comment but thought about that too.
WonderCsabo commentedon Jan 8, 2015
@yDelouis do you agree with the proposed enhancement?
yDelouis commentedon Jan 12, 2015
I'm okay with this.
WonderCsabo commentedon Jan 12, 2015
OK. Guys, i have other priorities, feel free to implement this.
WonderCsabo commentedon Feb 23, 2015
I am wondering about one thing: currently
EFragments uses the contextActivity'sFragmentManager. This is the same withFragment.getFragmentManager()if theEFragmentis directly atteched to theActivity. However, if it is nested, the two are not the same, since the second returns the parentFragment'sgetChildFragmentManager(). I think it makes more sense to useFragment.getFragmentManager(). @yDelouis @dodgex ?1 remaining item
WonderCsabo commentedon May 31, 2015
I started to implement this feature, and this came into my mind: does this feature actually make sense?
I mean if you want to use
getChildFragmentManager(), then you obviously used that to add theFragments. But when you added theFragments for example inonCreate(), then you already had the chance to store it in fields, so it is totally unnecessary to make AA inject them later. @wintersandroid what is the use case here? 😕mgod commentedon Jun 3, 2015
My use case is loading embedded nested fragments from the layout xml for a fragment. It seems like it should just fall back on getChildFragmentManager if we're on API >= 17 and the getFragmentManager wasn't able to find a fragment?
WonderCsabo commentedon Jun 3, 2015
@mgod does it work that way?
The fallback is a definitely no go, since it could be a source of bugs. We should leverage this decision to the client, with the proposed
childFragmentflag.mgod commentedon Jun 3, 2015
With the current AA, all of my
Fragments are null if I build them in the layout for another fragment. As a really dumb work around, this fixes the issue and gives me non-null fragments when I need them:I think the childFragment flag would work well, assuming it would continue to use the regular fragment manager in API < 17.
WonderCsabo commentedon Jun 3, 2015
Can you share the Fragment layout (skeleton) as well?
mgod commentedon Jun 3, 2015
Here's a cut down version of the layout:
WonderCsabo commentedon Jun 3, 2015
Thanks. I will test this first.
mgod commentedon Jun 3, 2015
Thanks @WonderCsabo! If it helps at all, I started running into the problem of the null fragments when I set my target API to 21 from 20. I also started seeing the issue in my robolectric tests after updating to robolectric 3.0-rc3 from 2.4.
mwinters-stuff commentedon Jun 3, 2015
My use-case is having a fragment in a view pager, where on a landscape tablet, there is two children (master/detail) and on phone or portrait there is only the master detail..
It would be ideal to just have the fragments loaded from the XML layouts, currently I have code in the @AfterInject to create the child fragments into frame layouts on the master fragment..
So Ideally..
(this is rough hand written code, I may have the fragment xml syntax wrong)
Then in my FragmentDualBalances.java
At the moment, in the above xml files the fragments are changed to FrameLayouts and my FragmentDualBalances.java is this
WonderCsabo commentedon Jun 3, 2015
Thanks guys! I have to be honest, i forgot that we can load the
Fragments into aFragmentfrom XML, so thoseFragments will be childFragments. In that case, this is indeed a valid use-case. I will test it, and then continue my implementation.WonderCsabo commentedon Sep 21, 2015
Done.
Mehly commentedon Sep 14, 2016
Is this issue really solved? I am currently running AA 4.1.0 and @FragmentById still returns null when using nested fragments?
mgod commentedon Sep 14, 2016
@Mehly I think this got removed from the final AA because nested fragments are only supposed to be supported when the fragment is loaded dynamically. I think they happened to work ok for a while, but start having problems in later Android versions. See the PR for more details.