Viewpager pages are not remain as such even if popstacking/backpressed

I have a fragment with ViewPager.The ViewPager contain 2 fragments(A and B).Fragment A contain an expandable list view.When I clicked an item in expandable list,it will got to another fragment.When I click back,it will popstack and show the the viewpager fragment.It works fine.But my problem is,when it is popstacked,the viewpager is reloading and it shows like the first time.I want to remain the expandable list view open as it is clicked when popstacked.I have spent lot of time on this.Can any one help me?

ReadingFragment ie.the main fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View mFragmentView = inflater.inflate(R.layout.fragment_reading, container, false);
     readingviewpager=mFragmentView.findViewById(R.id.readingviewpager);
    readingviewpager.setOffscreenPageLimit(2);
    readingviewpager.setAdapter(adapter);
    return mFragmentView;

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    adapter = new ReadingPagerAdapter(getChildFragmentManager()); //here used child fragment manager
}

ReadingPagerAdapter

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:

            FragmentA tab1 =  FragmentA .newInstance("A");

        return tab1;
        case 1:

            FragmentB tab2 =  FragmentB.newInstance("B");

            return tab2;

        default:
            return null;
    }

HomeActivity

private void ChangeFragment(BaseFragment fragment, String title, boolean isReplace) {
  //  mTitleTextView.setText(title);
    this.fragment = fragment;
    mFragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out);

    if (isReplace) {
        fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
    } else {
        fragmentTransaction.add(R.id.frame, fragment, CURRENT_TAG);
    }
    fragmentTransaction.addToBackStack(CURRENT_TAG);
    fragmentTransaction.commitAllowingStateLoss();
}

this is how I am changing the fragments

Archive from: https://stackoverflow.com/questions/59045010/viewpager-pages-are-not-remain-as-such-even-if-popstacking-backpressed

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *