0

I have an issue where the UI of a fragment doubles after a configuration change such that changing from light mode to dark mode and then scrolling down on the fragment's scroll view produces the image below. The UI will hover over each other producing a dizzying effect

enter image description here

The layout is as such:

 <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

           <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            .
            .
            .
           </RelativeLayout>
           <include
             android:id="@+id/distance_filter"
             layout="@layout/fragment_some_layout" />
        </LinearLayout>
 </ScrollView>

The host activity of the fragment has this method defined in its onCreate()

DistanceFragment mFragment = new DistanceFragment();
mFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(binding.rootContainer.getId(), mFragment).commit();

Interestingly using replace instead of add removes the bug but replaces the UI

Gil Ong
  • 67
  • 1
  • 6
  • I think moving the "DistanceFragment mFragment = new DistanceFragment();" outside "onCreate" (to NOT force it to create a new instance of an existing fragment) would solve your issue – JustSightseeing May 15 '23 at 08:43
  • @JustSightseeing I don't think I can do this since the activity will be restarted and all the other lifecycle callbacks will be hit – Gil Ong May 15 '23 at 09:29
  • Looks like this is a possible duplicate of https://stackoverflow.com/questions/18274732/android-fragments-overlapping-issue – Gil Ong May 15 '23 at 09:43

1 Answers1

0

Okay, I have resolved the issue but am unsure about the underlying cause. To start, it would be best to start here: Android: fragments overlapping issue

The solution in my case was simply setting a null check for savedInstanceState to prevent another fragment from being added. Why the activity overlaps the fragment, remains a mystery.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
DistanceFragment mFragment = new DistanceFragment();
mFragment.setArguments(getIntent().getExtras());

if (savedInstanceState == null){        
getSupportFragmentManager().beginTransaction().add(binding.rootContainer.getId(), mFragment).commit();
      }
}
Gil Ong
  • 67
  • 1
  • 6