How to reload / refresh fragment that has already been displayed once?
Asked Answered
H

2

5

enter image description here

  • I have 2 Fragments: My1stFragment and My2ndFragment.
  • Inside My1stFragment I have a ListView which is populated by database. Also when I click on edit button I call 2nd Fragment to edit data.
  • In 2nd Fragment I edit my salary amount and update my database by clicking on submit button and come back to My1stFragment.
  • The problem is when I come back to My1stFragment, my ListView is not updated, even though the database has been updated.
Hooten answered 3/4, 2015 at 10:2 Comment(2)
are you using a cursor loader?Lennox
i am getting data but i can not refresh List View.Hooten
K
2

Derive your PagerAdapter class as

public static class MyPagerAdapter extends FragmentStatePagerAdapter

instead of

public static class MyPagerAdapter extends FragmentPagerAdapter

Basically, FragmentPagerAdapter keeps the created Fragments 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 Fragments, else they won't be refreshed / updated.

2. Add

viewPager.setOffscreenPageLimit(0);

to your code. This ensures that adjacent Fragments 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.

Kennithkennon answered 3/4, 2015 at 10:4 Comment(3)
sure of the second point? code.google.com/p/android/issues/detail?id=56667#c3. By default its 1!.Lennox
great point, very good :) I suppose the second recommendation is redundant, as the minimum is internally set to 1.Kennithkennon
@ Y.S Thanks for helping its easy to understand and implement also i refer this simple, easy and good one.Hooten
G
5

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);
Gainsborough answered 3/4, 2015 at 10:12 Comment(2)
@RamMansawala Pleasure:)Gainsborough
Now it's depricated!Simony
K
2

Derive your PagerAdapter class as

public static class MyPagerAdapter extends FragmentStatePagerAdapter

instead of

public static class MyPagerAdapter extends FragmentPagerAdapter

Basically, FragmentPagerAdapter keeps the created Fragments 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 Fragments, else they won't be refreshed / updated.

2. Add

viewPager.setOffscreenPageLimit(0);

to your code. This ensures that adjacent Fragments 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.

Kennithkennon answered 3/4, 2015 at 10:4 Comment(3)
sure of the second point? code.google.com/p/android/issues/detail?id=56667#c3. By default its 1!.Lennox
great point, very good :) I suppose the second recommendation is redundant, as the minimum is internally set to 1.Kennithkennon
@ Y.S Thanks for helping its easy to understand and implement also i refer this simple, easy and good one.Hooten

© 2022 - 2024 — McMap. All rights reserved.