ViewPager without Fragments

In this blog post, i am going to explain how a viewpager can be used without Fragments. Fragments gathered some complaints from developers such as this. I encountered with some bugs and inconsistencies when using Fragments, too. Especially, when you use nested fragments, you may face with some interesting bugs. So you may also want to build apps without fragments. There are some alternative libraries to Fragments:

  1. Conductor and Mosby
  2. Flow and Mortar

If you have a few screens and not too much navigation, you may not want to use these libraries. This blog post may help you to construct an app without fragments and without alternative fragment libraries.

This blog post helped me too much in writing this post. This blog post shows building a viewpager without fragments,too. I will make a small contribution to this post and will show how to present and control views.

The app will have only an Activity. Below is the layout of the Activity:

This layout contains a View Pager. As you see in the code above, there are three includes. These are the pages of the View Pager. Each page contains a TextView displaying page number. Below is the step1 layout:

Step2 and Step3 layout files are identical with Step1.

We need a PagerAdapter for the View Pager. Below is the PagerAdapter implementation:

You need to override at least 4 methods in order to implement a PagerAdapter:

  1. instantiateItem(ViewGroup, int)
  2. destroyItem(ViewGroup, int, Object)
  3. getCount()
  4. isViewFromObject(View, Object)

instantiateItem returns the view that represents the page at the given position. You may inflate a view here and add it to ViewGroup which is a ViewPager in our case. I didnt add view to container because it is already added in the layout xml.

destroyItem removes a page that is at the given position. This is the place where a view is removed from the View Pager. i didnt override this method because i used setOffscreenPageLimit method in Activity. This prevents call of the destroyItem method.

getCount returns the page number of the View Pager

isViewFromObject this method looks whether a page View is associated with a specific key object which is returned at instantiateItem method. Since we return the view from instantiateItem method, it is enough to check just view == object.

addView method adds the presenter to PagerAdapter.

As you noticed i used Presenter class that controls and presents the views associated with pages. Below is the Presenter class:

You can extend this class and add more functionality. You may use this class as a lightweight Fragment. Step1Presenter is as follows:

Step2Presenter and Step3Presenter are identical with Step1Presenter. Now lets see the Activity:

Firstly ViewPager is initialized with offscreenPageLimit. Then presenters which contains and controls the views are added to PagerAdapter.

This completes the app. As you see, it is trivial to create an app without Fragments. Without Fragments, it is your responsibility to manage life cycle of views. In this sample, there are only 3 light views. In case of complex views, you may need to manage,add and remove views during navigation. For complex views, it is better to use alternative libraries like Conductor.

Source code of the sample:

admin Hakkında

Murat Aydin works as a senior software engineer at Arcelik and develops Android apps. He has several Android applications in Google Play. He is a Sun Certified Java Developer and has eight years of experience in developing web based applications using Java technologies, and desktop and engineering applications using .Net technologies. Murat Aydin earned his BSc degree in Computer Engineering from METU (Middle East Technical University) and his MSc degree in Software Engineering from METU. He is a member of GDG Ankara (Google Developer Group Ankara,, who organize several Android events in GDG Ankara, such as Android Developer Days. ( twitter: @maydintr gmail: maydin