片段回活动不工作

时间:2016-04-08 13:48:03

标签: android android-fragments

我在stackoverflow上有tried所有解决方案,但没有人帮助我。

当我从Default片段返回时,我想显示片段0,但此处应用程序已关闭

以下是MainActivity代码

   private void selectItem(int position) {

    switch (position) {
    case 0:
        getSupportFragmentManager()
                .beginTransaction().addToBackStack(PageSlidingTabStripFragment.TAG)
                .replace(R.id.content,
                        PageSlidingTabStripFragment.newInstance(),
                        PageSlidingTabStripFragment.TAG).commit();
        break;
    default:

        SherlockFragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content, fragment).commit();
        break;
    }

    mDrawerLayout.closeDrawer(mDrawerList);
}

Second Fragment back activity

public void onBackPressed() {
        FragmentManager fm = getFragmentManager();
        if(fm.getBackStackEntryCount() != 0) {
            fm.popBackStack();
            fm.getBackStackEntryAt(0);

        } else {
            getActivity().onBackPressed();
        }
    }

1 个答案:

答案 0 :(得分:0)

在onBackPressed()中改变你的条件:喜欢

Integer backStackSize = getFragmentManager().getBackStackEntryCount();

        if(backStackSize < 2){
            //new Logout_Dialog(MainActivity.this, MainActivity.this);
//YOU CAN ASK USER WHETHER HE WANTS TO LOGOUT OR NOT HERE......
        }
        else if(backStackSize >= 2){

            FragmentManager.BackStackEntry backEntry=getFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount()-1);


                fragmentManager.popBackStack();
        }//else if(backStackSize >= 2) closes here.....
        else
        {
            //Else fragment shud be removed from Backstack.... 
            super.onBackPressed();
        }//fragment shud be removed from Backstack..