I’m working on an android app which has different layouts for phone vs tablet. On the phone, my game activity layout works as a tabbed UI with 4 tabs, we’ll call them A, B, C, and D. I have a tabbar of buttons and a ViewFlipper with 4 fragments in it. So when they tap the C tab, the ViewFlipper shows that child which is FragmentC. FragmentC contains a ListView. When a user taps an item then I want to show a detail view about that item. So, FragmentC adds a new DetailFragment with a FragmentTransaction. This new DetailFragment covers FragmentC entirely. I’m basically trying to get a UINavigationController type of behavior if you are familiar with iOS. This works just fine on the phone.
On the tablet my game activity layout just splits the screen into 4 equal quadrants with FragmentA, FragmentB, FragmentC and FragmentD. That appears to work just fine, they all display just fine. However, when on the tablet UI, if I tap one of the items in the ListView in FragmentC the code is executed to add the new fragment, but nothing shows up on the screen.
My phone gameactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/viewRoot" ...>
<LinearLayout android:id="@+id/tabBarRoot" ...>
<Button ... />
<Button ... />
<Button ... />
<Button ... />
</LinearLayout>
<ViewFlipper android:id="@+id/viewFlipperMainMenu" ...>
<fragment class="FragmentA" ... />
<fragment class="FragmentB" ... />
<fragment class="FragmentC" ... />
<fragment class="FragmentD" ... />
</ViewFlipper>
</RelativeLayout>
My tablet gameactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/viewRoot" ... >
<LinearLayout ... >
<fragment class="FragmentB" ... />
<fragment class="FragmentA" ... />
</LinearLayout>
<LinearLayout ... >
<fragment class="FragmentC" ... />
<fragment class="FragmentD" ... />
</LinearLayout>
</LinearLayout>
FragmentC’s layout xml for both phone and tablet:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/viewRoot" ... >
<ListView android:id="@+id/listView" ... />
</LinearLayout>
When a List item in FragmentC is tapped, FragmentC will add a new DetailFragment with the following code:
public void pushNewFragment(Fragment fragment, String description) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.viewRoot, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(description).commit();
}
On the phone, this works just fine and DetailFragment shows up. On the tablet, nothing visibly happens when the pushNewFragment() method is called.
Any ideas?
Could it be that you have two
LinearLayoutswith anidof@+id/viewRootin your hierarchy (one in gameactivity.xml, one in Fragment C’s xml)?I’m thinking it might be attempting to attach the Fragment at the wrong point in the hierarchy for some reason, though I couldn’t say why this would only be a problem for one screen layout.
Edit:
It might be that your tablet’s gameactivity.xml simply contains no more room for further child views. i.e. You have a top level
LinearLayoutcontaining two childLinearLayouts. I’m guessing those childLinearLayouts have been configured withlayout_heightandlayout_widthto ensure they fill the parent. If you then add another view to the parentLinearLayout, it probably will have no remaining space in which to render.What if you wrap the parent
LinearLayoutin aFrameLayoutand give that the id of@+id/viewRoots that the new fragment can overlay the existing fragments?