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 8695069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:56:27+00:00 2026-06-13T00:56:27+00:00

I have a viewpager with 2 pages, on each fragment i put a button

  • 0

I have a viewpager with 2 pages, on each fragment i put a button to switching fragment,
but if i change orientation switching doesn’t work.
For switching fragment I using my OnChangePageButtonClick interface
Why is this happening?

ViewPager Activity:

public class ViewPagerMusic extends FragmentActivity implements OnChangePageButtonClick {

    private ViewPager vp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pager_music);

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);

        vp = (ViewPager)findViewById(R.id.viewpager);
        vp.setAdapter(viewPagerAdapter);
    }

    private class ViewPagerAdapter extends FragmentStatePagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
    public Fragment getItem(int position) {
        switch(position) {
            case 0:
                MainPage mainPage = new MainPage();
                mainPage.setOnChengeButtonListener(ViewPagerMusic.this);
                return mainPage;
            case 1:
                PlaylistPage playlistPage = new PlaylistPage();
                playlistPage.setOnChengeButtonListener(ViewPagerMusic.this);
                return playlistPage;
        }
        return null;
    }

        @Override
        public int getCount() {
            return 2;
        }

    }

    @Override
    public void selectPage(int page) {
        vp.setCurrentItem(page);        
    }

}

my Frgamnets:

public class MainPage extends Fragment {

    public MainPage() {
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.music_main_page, container, false);
        ImageButton btnPlaylist = (ImageButton)v.findViewById(R.id.btnGoPlaylist2);
        btnPlaylist.setOnClickListener(onButtonListener);
        return v;
    }

    private OnClickListener onButtonListener = new OnClickListener() {

        @Override
        public void onClick(View view) {
            onChangePageButtonClick.selectPage(1);
        }

    };

    public void setOnBackButtonListener(OnChangePageButtonClick onChangePageButtonClick) {
        this.onChangePageButtonClick = onChangePageButtonClick;
    }

    private OnChangePageButtonClick onChangePageButtonClick;

}

Playlist fragment is similar to MainPage fragment.

  • 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-13T00:56:28+00:00Added an answer on June 13, 2026 at 12:56 am

    Your fragments are storing a reference to the ViewPagerMusic activity. However, after rotation, the activity is destroyed and recreated, so the fragments now contain a reference to the old activity.

    Instead of storing a reference to the activity, you can call the activity’s method from the fragment like this:

    ((ViewPagerMusic) getActivity()).selectPage(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ViewPager with a dynamic number of pages in it. Each of
I have a ViewPager with 3 pages. In each page, I would like to
I have a ViewPager with 3 Fragments in there. Each Fragment uses a AsyncTaskLoader
I am using viewpager with action-bar tab and I am have two fragment in
I have a viewpager including 3 pages. I set one viewgroup in each page
I have this structure: Activity A has a ViewPager. The pages of a ViewPager
I have a ViewPager with multiple fragments. In one Fragment I play audio. When
I have implemented HorizontalListView inside ViewPager . ViewPager works fine but a child of
I have two tabs on my ActionBar and four pages in a ViewPager. I
I have a ViewPager FragmentActivity that holds 3 tabs, Each one of the tabs

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.