Search code examples
androidandroid-studioandroid-fragmentsbaseadapteronresume

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.


Solution

  • Use setUserVisibleHint(boolean isVisibleToUser).

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