I would have thought this was pretty easy, but it’s proving more difficult than I imagined. I just want to determine when both fragments are visible on the screen in one of my fragments. I’m not sure if onCreate is the best place to do it, but I tried onResume, onActivityCreated, onResume, onCreateView and onAttach. I’m thinking the fragments are aren’t actually visible, to the code, until later, but not sure.
This is the code I’m trying to use:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<fragment android:name="com.app.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>
<fragment android:name="com.app.Fragment2"
android:id="@+id/fragment2"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="fill_parent"
/>
</LinearLayout>
public class Fragment2 extends SherlockListFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
final Fragment1 f1 = (Fragment1)getFragmentManager().findFragmentById(R.id.fragment1);
final Fragment2 f2 = (Fragment2)getFragmentManager().findFragmentById(R.id.fragment2);
Boolean isVisible = (f1 != null && f1.isVisible() && f2 != null && f2.isVisible());
}
}
onStart()is when the fragment is visible to the user, so I suggest putting yourisVisiblecode inonResumejust to be safe.