FindFragmentById always returns null

I define the ID for my fragment in the xml layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/test_fragment" ... 

Then I add this snippet to the onCreate action:

 MyFragment myFragment = new MyFragment(); fragmentTransaction.add(R.id.fragment_container, myFragment); fragmentTransaction.commit(); 

Everything is working fine. Replacing fragments also works.

Later, I try to extract this fragment by its identifier in one of the activity methods:

 MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.test_fragment); 

This causes myFragment to be null . Always.

When I try to do the same with tags instead of IDs, I can easily get a snippet by tag:

 MyFragment myFragment = new MyFragment(); fragmentTransaction.add(R.id.fragment_container, myFragment, "testfragment"); fragmentTransaction.commit(); 

...

 MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentByTag("testfragment"); 

Why can't findFragmentById find the fragment, but findFragmentByTag does this? Did I miss something?

+49
android android-fragments
May 31 '12 at 12:31
source share
8 answers

R.id.test_fragment - this is not the identifier of your fragment, but the identifier of your LinearLayout

A call to add(int containerViewId, Fragment fragment) will add a fragment without a tag. So, either you use add(int containerViewId, Fragment fragment, String tag) and you return your fragment using your tag (as an identifier)

+36
May 31 '12 at 12:37
source share

Use the <FrameLayout> as a container in your layout file. Later, to replace fragments from your activity dynamically, you can use the container identifier <FrameLayout> to replace fragments in your activity.

 <FrameLayout android:id="@+id/test_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /> 
+9
Jun 20 '13 at 7:26
source share
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/test_fragment" 

it should be fragment not a LinearLayout

 <fragment android:name="com.example.yourfragment" android:id="@+id/test_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /> 
+3
May 31 '12 at 12:32
source share

I used android.support.v4.app.Fragment in my layout when calling getFragmentManager() , which was actually looking for subclasses of android.app.Fragment , and I got null . Therefore, the fix was to call getSupportFragmentManager() .

In general, make sure the package of the fragment that you subclass and use in your layout is the same as the corresponding FragmentManager that performs the search.

+3
Oct 12 '15 at 23:13
source share

R.id.test_fragment is your LinearLayout identifier, not your fragment.

You can also determine the id on the fragment when it is inflated from xml, as in this example http://developer.android.com/guide/topics/fundamentals/fragments.html#Adding

+2
May 31 '12 at 12:34
source share

Or, you should use instead:

 (MyFragment) getFragmentManager().findFragmentById(R.id.fragment_container); 
+1
Mar 14 '13 at 3:14
source share
 FragmentB fragmentB = (FragmentB) getSupportFragmentManager().findFragmentById(R.id.containerb); if(fragmentB != null) { fragmentB.showuserResponse(msg); } 

Use container identifier as fragment identifier. Then check the null reference.

0
Mar 31 '17 at 6:53
source share

fragmentTransaction.add (R.id.fragment_container, myFragment);

MyFragment myFragment = (MyFragment) getFragmentManager (). findFragmentById (R.id.test_fragment);

Pay attention to the difference.

You should pass R.id.fragment_container as the argument to findFragmentById as you pass it to the add function instead of R.id.test_fragment

By the way, according to the internal implementation of the two functions, it should be correct that the identifier may be its type of container.

-one
Nov 22 '13 at 12:19
source share



All Articles