Sunil Nagargoje
Sunil Nagargoje

Reputation: 11

Refresh fragment in Tab layout on any button click

In one of my app, I am using tab layout and viewpager with three fragments. In each fragment I am fetching data from server using Java Rest API. This server data depend upon fragment button click. E.g.

enter image description here In process fragment when I click on play button, then that card view goes into completed fragment, but it doesn't. when I refresh activity that time it goes, mean when activity close and then start. what should I do to refresh fragment?

Upvotes: 1

Views: 24417

Answers (5)

Tijo Thomas
Tijo Thomas

Reputation: 174

Try this in your Fragment class

 @Override
 public void setUserVisibleHint(boolean isVisibleToUser) {
 super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser) {
    // Refresh your fragment here
    }
}

Upvotes: 1

M.Usman
M.Usman

Reputation: 2119

Whatever operation you want to perform for refreshing fragment just put it inside one method and call it from onStart() method inside your fragment, worked for me.

Upvotes: 0

Kuldeep Kulkarni
Kuldeep Kulkarni

Reputation: 796

You can try to refresh fragment like this:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(YourFragment.this).attach(YourFragment.this).commit();

Upvotes: 9

Tishka17
Tishka17

Reputation: 320

I think, that you need to update Adapter on model changes, but not whole Fragment.

Upvotes: 0

user1517638
user1517638

Reputation: 982

Try this one ! Paste this code in your Activity.

viewpager.setOffscreenPageLimit(1);

Upvotes: -1

Related Questions