片段返回堆栈在活动中检索

时间:2016-06-02 12:01:37

标签: android android-fragments android-activity navigation-drawer

我有一个带抽屉的活动

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

     mFragmentManager = getSupportFragmentManager();

    .......

    result = new DrawerBuilder()
            .withActivity(this)
            .withToolbar(toolbar)
    ......
     .withOnDrawerNavigationListener(new Drawer.OnDrawerNavigationListener() {
                @Override
                public boolean onNavigationClickListener(View clickedView) {


                    if(getSupportFragmentManager().getBackStackEntryCount() > 0) {
                        getSupportFragmentManager().popBackStack();

                    }
                    //return true if we have consumed the event
                    return true;
                }
            })

    getSupportFragmentManager().addOnBackStackChangedListener(this);
    shouldDisplayHomeUp();

    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.content_frame, new DashboardFragment(), TAG);
    mFragmentTransaction.commit();
}

.......

@Override
public void onBackStackChanged() {
    shouldDisplayHomeUp();
}

public void shouldDisplayHomeUp(){
    //Enable Up button only  if there are entries in the back stack
    boolean canback = getSupportFragmentManager().getBackStackEntryCount() > 0;
    if(canback) {
        result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
    }else{
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
    }
}

在我的片段中......

 FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                folderName = list.get(position).getName();
                //fragmentTransaction.add(R.id.content_frame, innerFolder, folderName + fragmentManager.getBackStackEntryCount());
                fragmentTransaction.add(R.id.content_frame, innerFolder, folderName);
                fragmentTransaction.addToBackStack(folderName + fragmentManager.getBackStackEntryCount());
                fragmentTransaction.commit();
                getActivity().setTitle(folderName);

现在我试图弹出活动中的每个片段堆栈名称,当按下home up按钮时将其设置为标题栏。我需要按下的时候每个片段名称都是堆栈按钮。但是我被困住了这样做..任何人都可以帮助....

1 个答案:

答案 0 :(得分:1)

通过此功能,您将能够获得在addToBackStack

中设置的名称
FragmentManager fm = getSupportFragmentManager();
for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
    Log.e("backstack",""+fm.getBackStackEntryAt(i).getName());
    fm.popBackStack();
}