Fragment
s: My1stFragment
and My2ndFragment
. My1stFragment
I have a ListView
which is populated by database. Also when I click on edit button I call 2nd Fragment
to edit data.Fragment
I edit my salary amount and update my database by clicking on submit button and come back to My1stFragment
.My1stFragment
, my ListView
is not updated, even though the database has been updated.Add following in your firstFragment:
@Override
public void setUserVisibleHint(boolean isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
Log.e("Visible hint","in visibleHint ()");
//add your method to set adapter to listview here by passing (getActivity().getApplicationContext()) parameter for context
}
}
Now when you want to come to first fragment from other fragment, use :
((ActivityOfFragment) getActivity())./*viewPager if any.*/setCurrentItem(0/*fragment position*/, true);
Derive your PagerAdapter
class as
public static class MyPagerAdapter extends FragmentStatePagerAdapter
instead of
public static class MyPagerAdapter extends FragmentPagerAdapter
Basically, FragmentPagerAdapter
keeps the created Fragment
s in memory, while FragmentStatePagerAdapter
destroys and creates them anew as they are shifted in and out of view.
Further Considerations:
1. Make sure you are not calling setRetainInstance(true)
in any of your Fragment
s, else they won't be refreshed / updated.
2. Add
viewPager.setOffscreenPageLimit(0);
to your code. This ensures that adjacent Fragment
s are recreated.
3. Instead of
Activity -> ViewPager -> Fragments
create the structure as
Activity -> Fragment -> ViewPager -> Nested Fragments
This will ensure that each Fragment
is refreshed on page swipe. See this post for the implementation.
EDIT:
As discussed in the comments below, point 2 is redundant. So only points 1 and 3 are actually useful in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With