androidandroid-recyclerviewonsaveinstancestate

RecyclerView Data not visible when back button pressed or app closed


onSaveInstanceState and onRestoreInstanceState are not working. whenever i press back button or come out of the app the data of custom recyclerView is not visible until upload the next file. when i upload all the data comes back as I have stored it in shared preference.

Oncreate:

 if (savedInstanceState != null) {
            // Restore value of members from saved state
            savedInstanceState.get(String.valueOf(savedInstanceState));
        }
        else
        {
            //initialize members with default values for a new instance
            setContentView(R.layout.activity_resource);
        }
    @Override
    public void onSaveInstanceState(Bundle outState) {


        super.onSaveInstanceState(outState);
    }

    @Override
    public void onRestoreInstanceState(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onRestoreInstanceState(savedInstanceState, persistentState);
    }

Solution

  • onSaveInstanceState stores view state automatically if all views inside layout has id.

    For data restoring inside ListAdapter I would suggest sroring list inside ViewModel or at least Presenter depend on architecture you are using.