在popbackstack之后得到空片段

时间:2015-07-16 11:16:23

标签: android android-fragments

当我按下后退按钮时,片段为空。没有让先前的片段回过头来。试过很多解决方案,但没有成功。这是什么解决方案?我不知道如何解决这个问题。

private void displayView(int position) {
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = mHomeFragmentInstance;
        break;
    case 1:
        fragment = mOrderFragmentInstance;
        break;
    case 2:
        fragment = mOrdersheetFragmentInstance;
        break;
    case 3:
        fragment = mMyorderwidgetFragmentInstance;
        break;
    case 4:
        Intent intent=new Intent(this,LoginActivity.class);
        startActivity(intent);
        clearValues();
        finish();
        return;
    default:
        fragment = mHomeFragmentInstance;
        break;
    }

    if (fragment != null) {
        replaceFragment(fragment, position);
    }

}


/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

public void replaceFragment(Fragment fragment){     
    if (fragment != null) {
        Fragment currentFragment = this.getSupportFragmentManager().findFragmentById(R.id.frame_container);
        if(null != currentFragment ){
            if(currentFragment.equals(fragment)){
                return;
            }else{
                getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
            }
        }

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.frame_container, fragment);
        ft.addToBackStack(null);
        ft.commit();

    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

public void replaceFragment(Fragment fragment, int position){
    adapter.selectItem(position);
    Log.i("TAG", "replaceFragment:"+fragment.getClass().getName()+":"+position);
    replaceFragment(fragment);
    Log.i("TAG", "Add"+fragment.getClass().getName()+"backStackFragment");

    if(fragment.equals(mHomeFragmentInstance)){
        Log.i("TAG", "clear backStackFragment");
        backStackFragment.clear();
        backStackFragment.add(mHomeFragmentInstance);
    }

    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    setTitle(navMenuTitles[position]);
    mDrawerLayout.closeDrawer(drawerContainer);
}


@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    //getActionBar().setTitle(Html.fromHtml("<font color='#983A1B'>" + title + "</font>"));
    View view = getActionBar().getCustomView();
    //((TextView)view.findViewById(R.id.title)).setText(Html.fromHtml("<font color='#983A1B'>" + title + "</font>"));
    ((TextView)view.findViewById(R.id.title)).setText(Html.fromHtml("<font color='"+ ApplicationInfo.getPlfColor(this)+"'>" + title + "</font>"));
}

@Override
public void onBackPressed() {
    FragmentManager fm = getSupportFragmentManager();
    if(fm.getBackStackEntryCount() > 1) {
        fm.popBackStack();
    }else {
        //super.onBackPressed();
        finish();
    }
}

}

1 个答案:

答案 0 :(得分:0)

只需在您的片段之间转换时添加addToBackStack,如下所示:

fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).addToBackStack("tag").commit();

如果你编写addToBackStack(null),它会自己处理它,但如果你给一个标记,你应该手动处理它。