Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ViewPager, switching from fragment to another fragment

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.

like image 945
wilek Avatar asked Dec 09 '22 20:12

wilek


2 Answers

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);
like image 113
UgglyNoodle Avatar answered Dec 11 '22 10:12

UgglyNoodle


just create this method in your view pager

@Override

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

and then call this in your onclick method

((ViewPagerMusic) getActivity()).selectPage(1);

like image 32
Rishi Avatar answered Dec 11 '22 09:12

Rishi