Android Fragment Behaviour

Fahad Anjum

I have a doubt related to fragment. I have created one project which contains fragment(3). I am using viewpager for swiping from one fragment to another. When I go from one fragment (2) to anther (3) it will not add an item to list but when I go from 3 then 2 then 1 and returns back it will add that item. What is reason behind this. I just want to update my fragment when I go from one fragment to another.

Kushal Sharma

The behavior is because of setOffscreenPageLimit

The value by default is 1. and it cannot be 0.

when the vaule of setOffscreenPageLimit is default i.e 1, one page on both sides of the fragment will be loaded. So when you change data from B and then go to C, C was already loaded.. so changes are not reflected. On the other hand if you change data from B and then go to A, C will be unloaded. now if you go to B then C gets loaded and vaules refresh. Hope i could explain whats happening here..

When you swipe between two pages, there is a Point when both pages need to be visible, since you cannot swipe between two things when one of those does not even exist at that point.

also from comment (how to use it) : mViewPager.setOffscreenPageLimit(2); \will load 2 fragments on both sides of current fragment. by default the value is 1. (this is not the ans to your question though)

Edit :

actualy what i am doing is. in my fragment A or B there are lists. and for every item onclick there is an activity for details. when i open details for any item(i.e in new activity) there i can add/modify data(changes are reflected in db). Now when i press back i.e finish activity and return to main activity with fragments A B and C.. on Resume is called for all fragments. in my fragment C's on resume i called notifyDataSetChanged to my adapter.. so everythime onResume is called. the list in C is updated.

i am not sure if its a good solution, but it works for me.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive