Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh fragment when change between tabs

App open on first fragment and there is 2 tabs i want to refresh second fragment when i move to it but i don't want to refresh first fragment

MainActivity

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

}
private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new Main_Button(), "Main");
    adapter.addFragment(new TwoFragment(), "Main2");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

please explain your answer because i'm new in android and i don't know how i can do that

like image 753
fekra Avatar asked Jan 14 '17 22:01

fekra


People also ask

How do you refresh a fragment?

Go to your navigation graph and find the fragment you want to refresh. Create an action from that fragment to itself. Now you can call that action inside that fragment like so.


5 Answers

When a Fragment is made visible (i.e., the selected page in your ViewPager), its setUserVisibleHint() method is called. You can override that method in your TwoFragment and use it to trigger a refresh.

public class TwoFragment extends Fragment {

  @Override
  public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
      // Refresh your fragment here
    }
  }
like image 154
ianhanniballake Avatar answered Oct 28 '22 04:10

ianhanniballake


As of 2020 it is advisable to use architecture components(MVVM) such as LiveData and viewModel

This way all Fragments can share the same state

see docs here https://developer.android.com/jetpack/guide

like image 25
Barungi Stephen Avatar answered Oct 28 '22 04:10

Barungi Stephen


Inside Your fragment class use the below code:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    }
}
like image 45
megamind Avatar answered Oct 28 '22 02:10

megamind


Try This

ViewPager mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2); 
like image 34
Rahul Avatar answered Oct 28 '22 04:10

Rahul


Try this - it works for me

This code reloads current fragment, when it visible to user. Works when swiping and when back button pressed on next fragment.

@Override
    public void setUserVisibleHint(boolean isVisible) {
        super.setUserVisibleHint(isVisible);
        if (isVisible) {
                FragmentTransaction ftr = getFragmentManager().beginTransaction();
                ftr.detach(this).attach(this).commit();
        }
    }
like image 30
quarantine85 Avatar answered Oct 28 '22 03:10

quarantine85