Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 525
Ahlem Jarrar Avatar asked Aug 21 '16 22:08

Ahlem Jarrar


People also ask

Is ViewPager deprecated?

This function is deprecated.

How do I use ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

How do you not start all fragments together on ViewPager when Fragment A is selected?

Using an OnPageChangeListener in your View Pager, you can detect which fragment is currently shown in the View Pager. You'll then need to check which fragment is shown and then call a method on that fragment's class to start any sounds for example that you don't want to start until the fragment is the fragment in view.


2 Answers

Use setUserVisibleHint(boolean isVisibleToUser).

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

}
like image 108
Samvel Kartashyan Avatar answered Oct 31 '22 14:10

Samvel Kartashyan


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;
        }
    }
});
like image 44
kws Avatar answered Oct 31 '22 15:10

kws