40

Well, i got a simple <FrameLayout>:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FragmentContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Then in my code, i added a Fragment to it:

FragClass aFrag = new FragClass();
getSupportFragmentManager().beginTransaction()
        .replace(R.id.FragmentContainer, aFrag).commit();

And somewhere else in my code, i want to get that FragClass (extends Fragment) object from the ID R.id.FragmentContainer.

i have tried

((ViewGroup) findViewById(R.id.FragmentContainer)).getChildAt(0)

or

((FrameLayout) findViewById(R.id.FragmentContainer)).getChildAt(0)

but they are returning the View, instead of the Fragment attached to it.

i know i can keep the variable aFrag somewhere, so i do not need to find it again. But i believe there should be a way to retieve it.

midnite
  • 5,157
  • 7
  • 38
  • 52
  • 1
    http://developer.android.com/reference/android/app/FragmentManager.html#findFragmentById%28int%29 – user Mar 19 '13 at 06:20
  • Thanks for reply @Luksprog. But i cannot add an ID to aFrag. – midnite Mar 19 '13 at 06:22
  • No even if you use the method above with `R.id.FragmentContainer`? – user Mar 19 '13 at 06:23
  • 2
    Oh yes! Bingo! i've been asking stupid question (Sh...) The answer would be: `((aFrag) getSupportFragmentManager().findFragmentById(R.id.FragmentContainer))`. Thanks @Luksprog – midnite Mar 19 '13 at 06:28

1 Answers1

94

Let me wrap it up by a full answer :)

In this case, the dynamically added Fragment uses the ID of the container View (ViewGroup).

ref: http://developer.android.com/guide/components/fragments.html#Adding

Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it). There are three ways to provide an ID for a fragment:

  • Supply the android:id attribute with a unique ID.
  • Supply the android:tag attribute with a unique string.
  • If you provide neither of the previous two, the system uses the ID of the container view.

It is because it's a Fragment afterall. We have to use getSupportFragmentManager().findFragmentById() to retrieve it, which returns a Fragment, instead of findViewById() which returns a View.

So the answer to this problem would be:

((aFrag) getSupportFragmentManager().findFragmentById(R.id.FragmentContainer))

Thanks to @Luksprog.

Community
  • 1
  • 1
midnite
  • 5,157
  • 7
  • 38
  • 52
  • 1
    It may return null when container activity is launched? – MD TAREQ HASSAN Apr 05 '17 at 02:14
  • @HassanMakarov - Thanks for comment. Sorry that I have not been coding Android for a few years. So please take my reply with a grain of salt. As I have attached aFrag to FragmentContainer, why will it return null? Could you please explain more? – midnite Apr 05 '17 at 06:48
  • Fragment is not a subclass of View, so a container view can have the same id with a Fragment. Right? – Chao Apr 10 '17 at 08:19