简体   繁体   中英

Overiding onResume method in android not working in every fragment

I'm creating android app that have 3 fragments and I want refresh data every time I come back to the fragment. So, I override onResume() method in every fragment and add system out print to onresume to check if it's worked correctly.

But when I navigate to 2nd fragment it shows the add system out print of fragment 3 onresume. when I go to fragment 3 it not showing any add system out print. but when I came back to 2nd again it shows add system out print of fragment 1.

Please help me to fix this issue.

It appears you are using FragmentStatePagerAdapter in your ViewPager . It is the expected behaviour of the adapter that only neighbouring fragments are created. If you do not want this behaviour use FragmentPagerAdapter . But be aware of the memory taken up by all the fragments.

Make your fragments implement an interface:

public interface Listener {
    void resume()
}

Make your activity implement OnPageChangeListener :

viewPager.addOnPageChangeListener(this);

and then do the following:

@Override
public void onPageSelected(int position) {
    ((Listener) mAdapter.getItem(position)).resume();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM