fragment's isInLayout() returns false

android, android-fragments

Solution

When the fragment should return true from isInLayout method?

Fragment will return true when it is part of the layout defined via XML otherwise will return false. Relevant documentation part.

Problem

When should the fragment return true from `isInLayout()` method? In my case it returns false but I can see the fragment, and both `isVisible()` and `isAdded()` return true. In my activity's onCreate method I'm calling this: ``` FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); FragEventList listFrag = new FragEventList(); transaction.replace(R.id.fragment_container_1, listFrag, "list"); transaction.commit(); ``` and this is the layout: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" tools:context=".MainActivity"> <FrameLayout android:id="@+id/fragment_container_1" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> </LinearLayout> ``` Later when Loader returns the data, I want to pass the adapter to the fragment like this: ``` @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { Log.i(TAG, "onLoadFinished(Loader<Cursor> loader, Cursor data)"); Log.i(TAG, "fragment=" + getSupportFragmentManager().findFragmentByTag("list")); if (mAdapter != null && data != null) { mAdapter.swapCursor(data); FragEventList fragment = (FragEventList) getSupportFragmentManager().findFragmentByTag("list"); if (fragment != null) { Log.i(TAG, "in layout=" + fragment.isInLayout()); Log.i(TAG, "is added=" + fragment.isAdded()); Log.i(TAG, "is visible=" + fragment.isVisible()); } if (fragment != null && fragment.isInLayout()) { fragment.setAdapter(mAdapter); } } else { Log.v(TAG, "onLoadFinished: mAdapter is null"); onLoaderReset(null); } } ``` but `isInLayout()` returns false.

Original source