androidandroid-recyclerviewandroid-savedstate

how is the recyclerView maintains its scroll position without any extra coding


Created a simple activity which has a recyclerView and displays the data list. When turned on the setting's 'don't keep activity alive' and set 'Background process limit' to 'No background process'. Expect the acitivity will be killed by os in background.

When minimize the app and reopen it, saw the activity's onCreate() is called on a new instance and it does all again to create a new set of recyclerView and adapter with new datalist.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        mRecyclerView = findViewById(R.id.recyclerView);
        setSupportActionBar(toolbar);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
    }

But the ui still shows the item in last scrolled position (correctly restored).

Anyone knows how the previous position is transfered to the new recyclerView instance, etc.?

the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextColor="@android:color/white"
         />

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

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

    </ScrollView>
</LinearLayout>

the activity:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private ItemAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        mRecyclerView = findViewById(R.id. recyclerView);
        setSupportActionBar(toolbar);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager); 
    }


    @Override
    protected void onResume() {
        super.onResume();

        new FetchItemASyncTask(this).execute();
        // in the asyncTask it will do setup adapter when data ready
        // mAdapter = new ItemAdapter(dataList)
        // mRecyclerView.setAdapter(mAdapter);
    }
}

Solution

  • This is part of the default saving of state that Android performs for many views. The only thing you need to do to enable this is to give the view an android:id attribute (if you take that id away, you'll notice that it no longer saves the scroll position). The same is true for e.g. the text entered into an EditText.