如何处理backPress viewpager片段

时间:2019-06-25 20:25:40

标签: android android-fragments android-viewpager android-fragmentactivity android-tablayout

signup screen login screen我使用viewpager实现了auth屏幕,其中包含片段(loginFragment,SignUpFramgent), SignUpFragment包含三个用于注册步骤的片段 当用户在第一个注册片段中时,我还要处理每个注册片段上的后按,然后按后退开关以登录碎片

我的Auth活动

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

    //assign the views
    toolbar=findViewById(R.id.authToolBar);
    viewPager=findViewById(R.id.view_pager);
    tableLayout=findViewById(R.id.tabLayout);

    //add fragments to the adapter and attact them to viewPager
    TabAdapter tabAdapter=new TabAdapter(getSupportFragmentManager());
    tabAdapter.addFragment(new LoginFragment(),"Login");
    tabAdapter.addFragment(new SignUpFragment(),"Sign Up");
    viewPager.setAdapter(tabAdapter);
    tableLayout.setupWithViewPager(viewPager);

}

@Override
public void onBackPressed() {
    switch (viewPager.getCurrentItem()){
        case 0:
            finish();
            return;
        case 1:
            viewPager.setCurrentItem(0);
            return;
    }
    super.onBackPressed();
}

}

我的注册片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_sign_up, container, false);
    //assign the views
     button=view.findViewById(R.id.btn_sign_up);
     stateProgressBar=view.findViewById(R.id.your_state_progress_bar_signUp);
     fragmentManager=getChildFragmentManager();
     fragmentHelper =new FragmentHelper();
     //default fragment is PersonalInfoFragment
     if (savedInstanceState==null){

         fragment=new PersonalInfoSignUpFragment();
         fragmentHelper.setFragment(fragment,R.id.sign_up_container,fragmentManager,"personal");
         stateProgressBar.setStateDescriptionData(descriptionData);
         stateProgressBar.setCurrentStateNumber(StateProgressBar.StateNumber.ONE);
    }
    button.setOnClickListener(this);


    return view;
}


@Override
public void onClick(View v) {
    progressNumber=stateProgressBar.getCurrentStateNumber();
    //on click next move to next step until the last redirect to the mainActivity
    if (v.getId()==R.id.btn_sign_up){
        switch (progressNumber){
            case 1:
                fragment=new ContactSignUpFragment();

                fragmentHelper.setFragment(fragment,R.id.sign_up_container,fragmentManager,"contacts");
                stateProgressBar.setCurrentStateNumber(StateProgressBar.StateNumber.TWO);

                return;
            case 2:
                fragment=new ShopSignUpFragment();
                fragmentHelper.setFragment(fragment,R.id.sign_up_container,fragmentManager,"shop");
                stateProgressBar.setCurrentStateNumber(StateProgressBar.StateNumber.THREE);

                return;

            case 3:
                Intent intent=new Intent(getActivity(),MainActivity.class);
                startActivity(intent);
                getActivity().finish();

                return;
        }
    }

}

}

2 个答案:

答案 0 :(得分:0)

尝试:

@Override
public void onBackPressed() {

    if (viewPager.getCurrentItem() != 0) {
        viewPager.setCurrentItem(viewPager.getCurrentItem() - 1,false);
    }else{
         finish();
    }

}

但不要调用super.onBackPressed()方法。

答案 1 :(得分:0)

仅不要将Login片段添加到Backstack中。如果您有问题。

如果在单击Viewpagers的其他选项卡然后按Back时遇到问题,它将跳至您以前打开的所有片段-您可以执行以下操作:

operator[]