Ask Your Question
3

How can I maintain the three android fragments in Viewpager active?

asked 2023-07-16 02:06:32 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-16 02:08:02 +0000

plato gravatar image

To maintain the three Android fragments in ViewPager active, you need to follow these steps:

  1. Initialize your Viewpager with FragmentPagerAdapter or FragmentStatePagerAdapter.

  2. Override the getCount() method and return the number of fragments you want to display. In your case, it will be three.

  3. Implement the getItem() method and create a new instance of the appropriate fragment according to the position.

  4. Make sure you do not destroy any fragment when it goes out of the screen. To do that, you can override the destroyItem() method and remove the call to the super method.

  5. Ensure that you call mViewPager.setOffscreenPageLimit(2); to retain the three fragments in memory and avoid recreating them on scroll.

With these steps, you should be able to maintain the three Android fragments in ViewPager active.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-16 02:06:32 +0000

Seen: 12 times

Last updated: Jul 16 '23