Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fragment screens overlap

I've got an app that uses fragments. On one of my user's devices (the HTC One), the fragments overlap each other and his screen ends up looking like a mess:

enter image description here

I've tried to reproduce it on my own hardware, though it's not the HTC One. I've also tried using android version 4.1.2, which is the version he has and it works fine. Short of running out to buy an HTC One, does anyone have any suggestions?

When I add in the new fragment, I do this

 Fragment f = new MyFragment();
 FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
 ft.replace(R.id.mainContent, f);
 ft.addToBackStack(null);
 ft.commit();

My XML layout (trimmed up to relevant parts):

<RelativeLayout>
    <LinearLayout>
        <!-- My home screen content is here -->
    </LinearLayout>

    <!-- This is where the fragment gets placed -->
    <LinearLayout android:id="@+id/mainContent"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
    </LinearLayout>
</RelativeLayout>

UPDATE

The fragment that gets added has this layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/app_bg"    
    tools:context=".DeviceListActivity" >

    <!-- snipped! (for brevity) -->
</RelativeLayout>

I've been playing around - I noticed if I remove the android:background I can reproduce the problem which leads me to believe that the HTC One is causing the fragment's background property to be ignored for some reason.

like image 976
bugfixr Avatar asked Oct 03 '22 19:10

bugfixr


1 Answers

I have encountered similar situation and the following worked for me.

Check if the savedInstanceState if not-null, in that case, don't do the fragment transaction. It will resolve this issue.

if (homeFragment == null) {
    if (savedStateInstance != null) {
    homeFragment = (HomeFragment) fragmentManager.findFragmentByTag(HOME_FRAGMENT);
    return;
}
// tie new fragment
homeFragment = new HomeFragment();
homeFragment.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(homeFragment, HOME_FRAGMENT);
transaction.commit();
}

Hope this helps.

like image 82
jagmohan Avatar answered Oct 07 '22 20:10

jagmohan