Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8094207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:43:43+00:00 2026-06-05T20:43:43+00:00

How to create a new ViewPager everytime I remove the fragment(create it again)? My

  • 0

How to create a new ViewPager everytime I remove the fragment(create it again)? My pager is surviving the fragment destruction.

I have a ViewPager inside a Fragment, but when I remove the fragment from the FrameLayout where it is and the add it back. His ViewPager is the same of the old fragment instance(I do think it is) but all of the fragments are gone( empty pager is empty 🙁 ). How can I recreate the ViewPager again?

This code is on my onCreateView() method on my fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View fragment = inflater.inflate(getLayoutID(), container,false);
    ViewPager pager = (ViewPager) fragment.findViewById(R.id.myPager);

    PagerAdapter newAdapter = new MyPagerAdapter(getFragmentManager(), getFragments());
    pager.setAdapter(newAdapter);

    return fragment;
}

And I think that’s all you guys need to know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:43:45+00:00Added an answer on June 5, 2026 at 8:43 pm

    Now knowing I can’t have fragments inside fragments, I did this ViewPagerAdapter with an array of view that you can pass(could be anything you want) and show with it. Here comes the code:

    ViewPagerAdapter

    import java.util.List;
    
    import android.os.Parcelable;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    
    public class ViewPagerAdapter extends PagerAdapter{
    
        private List<View> pages;
    
        public ViewPagerAdapter(List<View> pages) {
            this.pages = pages;
        }
    
    
    
        @Override  
        public Object instantiateItem(View collection, int position) {  
            ((ViewPager) collection).addView(pages.get(position),0);  
    
            return pages.get(position);  
        }  
    
        @Override
        public int getCount() {
            return pages.size();
        }
    
        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    
        /**  
         * Remove a page for the given position. The adapter is responsible  
         * for removing the view from its container, although it only must ensure  
         * this is done by the time it returns from {@link #finishUpdate()}.  
         *  
         * @param container The containing View from which the page will be removed.  
         * @param position The page position to be removed.  
         * @param object The same object that was returned by  
         * {@link #instantiateItem(View, int)}.  
         */  
        @Override  
        public void destroyItem(View collection, int position, Object view) {  
            ((ViewPager) collection).removeView((View) view);  
        }  
    
        @Override  
        public boolean isViewFromObject(View view, Object object) {  
            return view==((View)object);  
        }  
    
        /**  
         * Called when the a change in the shown pages has been completed. At this  
         * point you must ensure that all of the pages have actually been added or  
         * removed from the container as appropriate.  
         * @param container The containing View which is displaying this adapter's  
         * page views.  
         */  
        @Override  
        public void finishUpdate(View arg0) {}  
    
        @Override  
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}  
    
        @Override  
        public Parcelable saveState() {  
            return null;  
        }  
    
        @Override  
        public void startUpdate(View arg0) {}  
    }  
    

    And here comes an extra abstract fragment where you could just override the abstract methods and voila:

    AbstractPagerFragment

    import java.util.List;
    
    import templateDigital.util.ViewPagerAdapter;
    
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public abstract class AbstractPagerFragment extends Fragment {
    
        private ViewPager pager;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
    
            View fragment = inflater.inflate(getLayoutID(), container,false);
            setRetainInstance(false);
            PagerAdapter mPagerAdapter = new ViewPagerAdapter(getViews());
            pager = (ViewPager) fragment.findViewById(getPagerID());
            pager.setAdapter(mPagerAdapter);
            pager.getAdapter().notifyDataSetChanged();
    
            return fragment;
        }
    
    
        public abstract List<View> getViews();
    
        public abstract int getLayoutID();
    
        public abstract int getPagerID();
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            pager = null;
            System.gc();
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tryed to create new components from one base Windows Form, also I found
I want to create a horizontal scrollable ViewPager. My ViewPager contains Fragments. Each Fragment's
I'm currently looking into unit testing for a new application I have to create.
I have a viewpager that pages through fragments. My FragmentPagerAdapter subclass creates a new
The example for viewpager, here , contains the lines: mViewPager = new ViewPager(this); mViewPager.setId(R.id.pager);
I create new desktop with CreateDesktop and want to get it's DC & RC.
I create new blank solution. Add exist web site. Structure: Solution E:...\projectname Folders: bin
Code to create new form instance of a closed form using form name I
I want to create new WCF service and client. The 2 parties will communicate
How to create new type in c#? For example byte has range 0 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.