ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

Find a fragment in stack #61

Closed eoboite closed 7 years ago

eoboite commented 7 years ago

Hello,

I really like FragNav it's working great in my app. My confusions come in when I need to find a fragment in a stack. Right now I have two fragments (Fragment A and B). I created an interface in Fragment B which will pass data back to Fragment A. My Activity implements the Listener created in Fragment B, but I run into the issue of finding Fragment A in the stack to pass the data to it.

Anyway to access the getSupportFragmentManager() in FragNav to findViewById() ?

ncapdevi commented 7 years ago

Hey, happy to help, but could you post a real quick example of exactly what it is that you're trying to do.

I'm imagining something like

public class FragmentA extends Fragment{
}
public class FragmentB extends Fragment{

public interface someListener{}
}
public class Activity extends Activity implements someListener{
}

I think is what you are saying. It seems like you want to grab the fragmentStack, and find which fragment specifically is FragmentA, is that correct? If it is correct, are you checking to see that it is a specific instance of FragmentA, or just ANY instance of FragmentA?

If it's ANY instance of the fragment, then you can simply do

Stack<Fragment> fragments =   mFragNavController.getCurrentStack();
//or you could do mFragNavController.getStack(desiredIndex);

and then iterate through each of those fragments to see if it is an instanceof FragmentA

eoboite commented 7 years ago

Hey,

Thank you, and yes that's exactly what I'm trying to do. Just realized you're using a Stack<> to hold the Fragments. Maybe I can use the .search(Object o) to find the Fragment?

ncapdevi commented 7 years ago

@eoboite just updated my comment above for the ANY instance example. I think that's clear? If it's not, let me know. There's a few different ways that you could do it if you were looking for a specific instance of a fragment if that's what you need.

eoboite commented 7 years ago

Okay thank you, I'll try that out. Appreciate your help.

ncapdevi commented 7 years ago

No problem, happy to help. I'm going to close this question, but if you need anything else don't hesitate to comment back here or create a new issue. Cheers.