Fragment's are not calling OnResume() when swiping with ViewPager
Asked Answered
R

4

9

Explaining my problem :

I spend much time but I can not get this to work.I have view pager in main activty that contains three fragments using (Tabhost).My ViewPagerAdapter class extend FragmentStatePagerAdapter.

The problem I'm facing that my OnResume() Method is not called when I swipe the View .And I want to update the view of viewpager's fragment on swipe.

My OnResume() method is only called when i click on the ListView item and back again . but when I press OnLongClick on the ListView other fragments are not refreshed .

Note : I know that this question was asking before but none of those solutions helped me .

Note 2: When my phone goes to sleep then after unlocking phone 2nd fragment calling onResume()

My OnResume() Method in the first tab :

 @Override
public void onResume() {
    super.onResume();

    adapterLogin.notifyDataSetChanged();
}

My OnResume() Method in the second Tab:

 @Override
public void onResume() {
    super.onResume();
   adapterLogin.UpdateView(databaseHelper.getAllVoitureFavourite(1,username));
   adapterLogin.notifyDataSetInvalidated();
   adapterLogin.notifyDataSetChanged();


}

My UpdateView() Method in the BaseAdapter :

public void UpdateView(List<Voiture> items) {
    this.voitureList = items;
    notifyDataSetInvalidated();
    notifyDataSetChanged();

}

Screen shot of my App for more understanding mu issue :

enter image description here

Any help will be appreciated.

Rab answered 21/8, 2016 at 22:44 Comment(0)
M
11

Use setUserVisibleHint(boolean isVisibleToUser).

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
       // Do your stuff here
    }

}
Mage answered 27/1, 2017 at 13:2 Comment(2)
override this method in the fragmentMannerless
Overrides deprecated method in 'androidx.fragment.app.Fragment'Salicin
G
1

The behavior you describe is how ViewPager works, there is nothing wrong with it.

Also, if you check the source code of the ViewPager class you will notice that the minimum offscreenPageLimit is 1. Setting it to 0 simply does nothing as it falls back to the default value 1.

What you can do is to add a TabHost.OnTabChangeListener so that on each swipe have the appropriate method called.

mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        switch (mTabHost.getCurrentTab()) {
            case 0:
                //fragment 1 update()
                break;
            case 1:
                //fragment 2 update()
                break;
            case 2:
                //fragment 3 update()
                break;
        }
    }
});
Glaucoma answered 22/8, 2016 at 0:4 Comment(0)
O
1

if you set offscreen page limit = 0 then this on swipe onviewcreate or onviewcreated method also call again and again this may disturb the flow of your app best approach which I use is using interface method override in your fragment and on page change invoke this override method that will work fine for you

Obese answered 6/9, 2022 at 5:16 Comment(0)
S
0

If your ViewPager only has 2 pages, then neither of the fragments will pause during a swipe, and onResume() will never be called.

I believe that it always retains the neighbor pages by default, meaning it has a page limit of 1.

You could try setting the number of pages that it retains to 0.

mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(0); 
Sather answered 21/8, 2016 at 22:51 Comment(1)
Check if your first page refreshes when you swipe to page 3 and back and let me know. Make sure you log your onResume() function, because if your data isn't changing, then you won't notice it refreshing. Also, careful in your onResume() method for the 2nd Tab. It appears your calling notifyDataSetInvalidated() twice which is unnecessary.Sather

© 2022 - 2024 — McMap. All rights reserved.