How Can I Maintain The Position Of Listview When User Comes Back To The Activity By Pressing Back Button?
I have a listview and How can I maintain the position of my ListView in my activity when I go to another activity (by launching another intent) and then come back (press the back b
Solution 1:
You could,
Use Intent.putExtra to pass on the selected index to the SecondActivity and then back to First.
Use SharedPreferences to store and retreive the index.
I could elaborate, if you want.
Solution 2:
intindex= mList.getFirstVisiblePosition();
Viewv= mList.getChildAt(0);
inttop= (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());
top
gives you visible position. Then you can save it in onSaveInstanceState()
(I don't remember exact implementation)
@OverridepublicvoidonSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putInt("tag", top);
}
Then restore you position in onCreate and set it to list
mList.setSelectionFromTop(index, top);
Solution 3:
kotlin way
val index: Int = recyclerView.scrollState
recyclerView.scrollToPosition(index)
Post a Comment for "How Can I Maintain The Position Of Listview When User Comes Back To The Activity By Pressing Back Button?"