Android - Recycler view resizes when app is on background Android - Recycler view resizes when app is on background xml xml

Android - Recycler view resizes when app is on background


I have solved the problem by breaking part the included layouts. So instead of adding an included layout, I just transferred all the included in to the main XML layout.

Even though this fixed the problem, i still do not know why the bug occurred in the first place.


If you are using only single child view, it's better to use FrameLayout. Then in RecyclerView use match_parent parameters in layout attribute, so your code should look like this:

<?xml version="1.0" encoding="utf-8"?><FrameLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/linearLayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/transparent">    <android.support.v7.widget.RecyclerView        android:id="@+id/recyclerview"        android:layout_width="match_parent"        android:layout_height="match_parent"/></FrameLayout>

By the way my best method to debug scenarios like this is to write android:background="#0000FF" in every view, to see how it inflates.


  • You don't need to first remove the old fragment if you are going to call replace(R.id.container_id, newFragment);
  • You don't need to call commit after every change (remove(), replace(), etc.), call all your changes then end it with a commit()
  • If your activity did not lose/change its state (e.g. rotate), then you are just removing and adding the same fragment for no reason
  • You are checking if it is a MenuFragment in the if statement, then, oddly enough, if the condition holds, you go and re-add the MenuFragment, when you should probably replace it if it's not a MenuFragment

    if (!(fragment instanceof MenuFragment)) {    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();    Fragment menuFragment = new MenuFragment();    // pass any data...    transaction.replace(R.id.fragment_container, menuFragment);    transaction.addToBackStack(null);    transaction.commit();}

Adding Log.d(TAG, "method name") in your onResume(), onPause() might help you know what's happening.

Also, checking the view hierarchy in Tools > Layout Inspector might also help.

Enabling Settings > Developer Options > Show Layout Bounds on your emulator might also help.

Let me know if I had anything wrong. Also let me know if any of this works for you.