Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the cached memory of Viewpager with Tablayout

I just started using ViewPager and Tablayout from Material Design (comes with Android Studio). I created 5 fragments and I am able to use an swipe them as needed.

Based on user selection in Fragment2 I want to remove Fragment3. However, after removing the Tab from the tablayout, when swiping to the 3rd tab (which was the 4th tab before the removal of the 3rd tab) the wrong fragment is displayed - the system has the 3rd Fragment in memory and displayes it. If I destroy that fragment then I get an exception about "Can't change tag of fragment".

How can I tell the system that the 3rd fragment is not to be used or to remove it from memory until I need it and then to reload it to memory.

like image 645
Zvi Avatar asked Mar 09 '16 07:03

Zvi


2 Answers

Are you using FragmentPagerAdapter? If so, you might wanna try FragmentStatePagerAdapter. Because it will destroy the unused fragments. If not, provide some code so we might be able to dive in deeper.

like image 153
Vucko Avatar answered Nov 18 '22 05:11

Vucko


I also had the same issue, that was, although I updated the data of the adapter, the view still did not updated! However, I found, once I updated the data for the adapter, and called .notifDataSetChange(), I called .setsAdapter(m_PagerAdapter) AGAIN. That worked for me :) ...

//
// update the data of adapter ...
//
m_PagerAdapter.notifyDataSetChanged();
m_vpgItems.setAdapter(m_PagerAdapter);
like image 20
Seth Zhang Avatar answered Nov 18 '22 05:11

Seth Zhang