简体   繁体   English

从新片段返回到viewpager状态

[英]Return to viewpager state from a new fragment

I have a layout that has a viewpager. 我有一个带有viewpager的布局。 The viewpager has 2 fragments inside it. viewpager内部有2个片段。 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 : 到目前为止,我是通过在片段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. 现在,当我按下后退按钮时,我无法进入上一个Viewpager页面,并且无法保留其先前状态。 I am thrown out of the activity on back press. 我被压在后面的活动上了。 This is my implementation of OnBackPress in the main activity: 这是我在主要活动中对OnBackPress的实现:

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();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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