Reputation: 2866
I'm trying to implement nested fragments
but I have a problem.
When I add 2 different fragments
with two different tags
("tag1", "tag2") in a ChildFragmentManager
of one fragment
, everything seems to be ok.
But when I call getChildFragmentManager().findFragmentByTag
on these tags
, only the first fragment
is returned. I'm getting null
on the second one.
To add fragments
I use:
PersonsImageFragment imf = new PersonsImageFragment(imgPreview, 1);
android.support.v4.app.FragmentTransaction transaction = frag.getChildFragmentManager().beginTransaction();
transaction.add(R.id.frame, imf, "tag1")
.addToBackStack(tagName)
.commit();
imf = new PersonsImageFragment(imgPreview, 2);
transaction.add(R.id.frame2, imf, "tag2")
.addToBackStack(tagName)
.commit();
Am I missing something ?
EDIT:
f = getChildFragmentManager().findFragmentByTag("tag1");
// f.onActivityResult(requestCode, resultCode, data);
f = getChildFragmentManager().findFragmentByTag("tag2");
// f.onActivityResult(requestCode, resultCode, data);
EDIT 2:
What I'm trying to do is that, I have ViewPager
and in one of the fragments
I have custom view
, that is responsible for taking a picture and showing it in the ImageView
. So the nested fragment
is calling camera intent
and in onActivityResult
it is updating the ImageView
. As you know there's a bug
in Nested Fragments
, I have to call manually the onActivityResult
of the nested fragment
from the Parent Fragment
. So in Parent's onActivityResult
I'm trying to get the nested fragment
I need and call its onActivityResult
.
EDIT 3:
As I said, I have Custom View
, that is responsible for taking picture and showing it off. I have 3 Custom Views
in the Parent Fragment
Layout
. And when the user clicks any of them, it should take a picture and preview it. If I add only 1 custom view
to the parent fragment
it works, but when I use 3 of them, the click event
works on all of them, that mean that camera fragment
is attached but I can't call onActivityResult
on other two because I can't get the fragment
that startedActivityForResult
, it is null
. So the problem, in my opinion, should be in adding nested fragments
to transaction
but I can't find it.
Upvotes: 1
Views: 1731
Reputation: 5011
I can't really be sure since the source code is not 100% complete, but I think there a couple of things being mixed up.
For example, you're reusing the same PersonsImageFragment
for both fragments and also the transaction. Try something like this:
PersonsImageFragment frag1 = new PersonsImageFragment(imgPreview, 1);
android.support.v4.app.FragmentTransaction transaction = frag1.getChildFragmentManager().beginTransaction();
transaction.add(R.id.frame, frag1, "tag1")
.addToBackStack(tagName)
.commit();
PersonsImageFragment frag2 = new PersonsImageFragment(imgPreview, 2);
android.support.v4.app.FragmentTransaction transaction2 = frag2.getChildFragmentManager().beginTransaction();
transaction2.add(R.id.frame2, frag2, "tag2")
.addToBackStack(tagName)
.commit();
Haven't tried it out so you might need to modify it a bit to compile, but you get the idea. Let me know if this works.
Upvotes: 3