-1

I have the fragment obj as well as the FrameLayout, I could also complete my logic if I can:

Get my fragments containerViewId

OR

See what FrameLayout a fragment is in?

2 Answers2

1

do this:

getSupportFragmentManager().findFragmentById(R.id.your_frame_layout);

it returns the fragment in frame layout

UPDATE

to get all fragments that you added in the frame layout (not all frame layouts):

        FragmentManager mFragmentManager = getSupportFragmentManager();
        mFragmentManager.beginTransaction().add(R.id.your_frame_layout, /*your Fragment*/).commit();

        //get all fragments that you added to mFragmentManager
        List<Fragment> fragmentList = mFragmentManager.getFragments();
Ehsan msz
  • 1,774
  • 2
  • 13
  • 26
  • Thank you for the response Ehsan, this returns the last fragment added to the frame layout not all that have been added. Is there a way to do that? – Jose Flores Nov 01 '19 at 20:40
  • 1
    @JoseFlores you should **add** your fragments to fragment manage (don't replace) and you can get a list of all by calling `getSupportFragmentManager().getFragments()`, it returns a list of `Fragment`. – Ehsan msz Nov 01 '19 at 20:47
  • The problem with .getFragments() is that it will return fragments in other FrameLayouts as well ... – Jose Flores Nov 01 '19 at 20:54
  • @JoseFlores you can do this : `FragmentManager mFragmentManager = getSupportFragmentManager()` now you can use `mFragmentManager` on your frame layout. and for get all fragments `mFragmentManager.getFragments()` it returns only the fragments that you added with `mFragmentManager` – Ehsan msz Nov 01 '19 at 21:04
  • [`getId()`](https://developer.android.com/reference/androidx/fragment/app/Fragment#getId()) returns the id of the FrameLayout the Fragment was added to, allowing you to filter the list of Fragments. – ianhanniballake Nov 01 '19 at 21:09
0

(FrameLayout) fragment.getView().getParent() give it a try. it may give you frame layout that fragment attached to.

Eren Tüfekçi
  • 2,463
  • 3
  • 16
  • 35