I am developing android application with multiple fragment in a activity. I use grid view inside fragment. When I change fragment and then pop back stack to old fragment, the grid view scroll position was changed to position zero. How to keep grid view scroll position after fragment pop back stack?
Asked
Active
Viewed 3,229 times
2
-
don't set grid view adapter in onResume(). – Sanjay Kakadiya Apr 18 '16 at 04:53
-
Your problem is closely related to this question: http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview/3035521#3035521. Check it out. It is about a ListView but it seems (After a quick inspection) all methods used come from AbsListView which GridView also inherits from. – Machinarius Apr 18 '16 at 04:54
-
Can you Share your code where you set adapter, may be your adapter reset when pop menu close. – Rohitashv jain Apr 18 '16 at 05:13
-
When you add next fragment, dont replace it instead make it add and set addToBackStack – silentsudo Apr 18 '16 at 05:36
-
@sopheak and forget to mention make bg of this layout white and or whatever color you are giving to just doesn't seem overlap and make root layout clickable true – silentsudo Apr 18 '16 at 05:37
-
Do not initailize your grid adapter onCreateView() .. do it in onCreate method of the fragment.. That will do the trick :) – mudit_sen Apr 18 '16 at 05:41
-
@Rohitashv jain ! I set GridView adapter onCreateView() function. GridVIew adapter always reset when fragment popBackStack. – Sopheak Apr 18 '16 at 05:48
-
@sector11 ! If I do not use replace function and use add function. how about memory using? – Sopheak Apr 18 '16 at 05:49
-
@Sopheak - your onCreateView method recall after popBack Stack? – Rohitashv jain Apr 18 '16 at 06:06
-
@Rohitashv jain - yes – Sopheak Apr 18 '16 at 06:11
-
if only recall onCreateView Method not onCreate method after pop back stack, you can apply an check in onCreateView Method. adapter initialize into onCreateView method first check adapter is null then initialize and set adapter other-vice follow else condition as you want. if(mAdapter==null) { // initialize and set adapter here } – Rohitashv jain Apr 18 '16 at 06:15
-
@Sopheakthey have given add method may your usecase is the best fit for it did it work? – silentsudo Apr 18 '16 at 06:42
-
@Rohitashv jain - Thank you. your solution is good. but when I scroll to end of GridView (last GridView Item) and then go to next fragment, It was wrong scroll position after back stack fragment. other right scroll. Do you have any idea? – Sopheak Apr 18 '16 at 06:49
1 Answers
9
Well, do this
in you onCreateView() method
GridAdapter yourGridAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
if(yourGridAdapter ==null){
yourGridAdapter = new GridAdapter();
}
GridView gridView = (GridView) convertView.findViewById(R.id.your_grid_view);
gridView.setAdapter(yourGridAdapter);
...
}
Explanation:
When a fragment is reloaded from pop back stack on its onCreateView() and method after in fragment life cycle till onResume() get called and if you re-instantiate the adapter in onCreateView() method the gridView items will be created again but if you wont instantiate it the same adapter will be attached to the gridView.

mudit_sen
- 1,470
- 15
- 24
-
Thank for your solution. I can keep scroll position. but when I scroll to end of GridView (last GridView Item) and then go to next fragment, It was wrong scroll position after back stack fragment. otherwise right scroll position. Do you have any more idea? – Sopheak Apr 18 '16 at 06:43
-
Using GridLayoutManager with RecyclerView may help because i don't have much idea about GridView – mudit_sen Apr 18 '16 at 08:26