How to save the UI state of optional GridLayoutManaer RecyclerView in saveInstanceState

How to save the UI state of optional GridLayoutManaer RecyclerView in saveInstanceState



I have a RecyclerView which uses GridLayoutManager and different adapters upon selected menu item. I want to save the state of last displayed items of the RecyclerView, so that user can see the same upon configuration changes. The UI doesn't have to fall back to the default UI.



Below is my MainActivity which I want to save its UI.


@Overrideprotected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDb = AppDatabase.getInstance(getApplicationContext());
Intent intent = getIntent();
if (intent.hasExtra("Favourite"))
int favouriteMovie = intent.getIntExtra("Favourite", -2);
if (favouriteMovie == 1)
loadFavouriteMovie();


movies = new ArrayList<>();
favouriteMovies = new ArrayList<>();
mLoadingIndicator = findViewById(R.id.loading_indicator);
mImageGrid = findViewById(R.id.rv_movies);
layoutManager = new GridLayoutManager(getApplicationContext(), 2);
mImageGrid.setLayoutManager(layoutManager);
mImageGrid.setHasFixedSize(true);
mAdapter = new
ImageGridAdapter(this, movies);
mImageGrid.setAdapter(mAdapter);
DividerItemDecoration decoration = new
DividerItemDecoration(this, VERTICAL);
mImageGrid.addItemDecoration(decoration);
DividerItemDecoration decoration2 = new
DividerItemDecoration(this, HORIZONTAL);
mImageGrid.addItemDecoration(decoration2);
loadMoviesTask();
//populateMovieData();

@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState)
super.onRestoreInstanceState(savedInstanceState);
if ((savedInstanceState != null) && (savedInstanceState.getParcelable(STATE_KEY) != null))
listState = savedInstanceState.getParcelable(STATE_KEY);


@Overrideprotected void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
listState = layoutManager.onSaveInstanceState();
outState.putParcelable(STATE_KEY, listState);



I am aware this can be achieved using key and saving it in database and at onCreate, RecyclerView can be generated by querying the database with the saved key, but I don't want to be querying database at every onCreate: I want to have the state saved using onSavedInstanceState.



I have tried with the code above, but the app wouldn't just do what I want.






Don't apologize for code formatting, fix it. If you don't even bother making your code readable for anyone who tries to help you... why should they bother?

– James Z
Sep 8 '18 at 4:49






plz make the code clear to read :-

– aminography
Sep 8 '18 at 19:40






The code is now edited

– Show Young Soyinka
Sep 8 '18 at 23:45




0



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)