1

I learned from here how to prevent restart on orientation change How to avoid restarting activity when orientation changes on Android

Thus I implemented it

In my Manifest I make sure rotate doesn't trigger restart

android:configChanges="keyboardHidden|orientation|screenSize"

My layout is very simple

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scroll">

        <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/list_of_items"/>

    </ScrollView>

    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/add"
            android:layout_alignParentBottom="true"/>

</RelativeLayout>

In my MainActivity I set

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.activity_main);
}


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    RecyclerView itemsView = findViewById(R.id.list_of_items);
    itemsView.setLayoutManager(new LinearLayoutManager(this));
    itemsView.setNestedScrollingEnabled(false);

    ItemsAdapter items = new ItemsAdapter(this);
    itemsView.setAdapter(items);
}

Whenever rotate happens I checked inside onConfigurationChanged the item Count and visibility state, and log prints that the recycler view is visible and that it contains x items, yet somehow on rotate it never shows those items. What am I missing? My ItemsAdapter is a really simple adapter with nothing fancy inside.

Quillion
  • 5,757
  • 10
  • 55
  • 83
  • By using `setContentView(R.layout.activity_main)` in your `onConfigurationChanged()` you basically create new view every time the device is rotated. But you only set the adapter once, for the view which you've found in your `onCreate()`. Deleting this line from `onConfigurationChanged()` should fix your problem. And by the way, most likely you need ScrollView, RecyclerView handles it's own scrolling. – Jan Stoltman Aug 19 '18 at 18:50
  • @JanStoltman I will accept it as correct answer if you post it as one. It worked!!! – Quillion Aug 19 '18 at 18:53

1 Answers1

1

By using setContentView(R.layout.activity_main) in your onConfigurationChanged() you basically create new view every time the device is rotated. But you only set the adapter once, for the view which you've found in your onCreate(). Deleting this line from onConfigurationChanged() should fix your problem.

And by the way, most likely you need ScrollView, RecyclerView handles it's own scrolling.

Jan Stoltman
  • 355
  • 3
  • 15