简体   繁体   中英

Return to viewpager state from a new fragment

I have a layout that has a viewpager. The viewpager has 2 fragments inside it. When I click on the second page I want a new fragment to pop up and fill the entire screen. I did this so far by using the following in the fragment B :

FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, new FragmentA(),"current"); 
fragmentTransaction.commit();

Now when I press the back button I cant get to the previous viewpager page and cannot retain its previous state. I am thrown out of the activity on back press. This is my implementation of OnBackPress in the main activity:

Fragment fragment = getSupportFragmentManager().findFragmentByTag("current");
    if(fragment != null)
        getSupportFragmentManager().beginTransaction().remove(fragment).commit();

    if(vp.getCurrentItem() == 0)
    {
        super.onBackPressed();
    }
    else
    vp.setCurrentItem(0);

have to tried this,

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().onBackPressed();
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM