2

A --> 列表片段

B --> 列表片段(从 A 调用)

C --> 与 B 相同(从 B 调用)

我正在使用这些函数从 A 调用 B ;

>   FragmentManager fm = getFragmentManager();
>               if (fm != null) {
>                   FragmentTransaction ft = fm.beginTransaction();
>                   ft.replace(R.id.fragment_content, entryListFragment);
>           ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
>                   ft.addToBackStack(null);
>                   ft.commit();

这是我的 FragmentActivity 课程;

public class FragmentTabActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fragment_tab);

        Commons.globalContext = this;    
        FragmentManager fm = getSupportFragmentManager();
        TabFragment tabFragment = (TabFragment) fm.findFragmentById(R.id.fragment_tab);
    }


    @Override
    public void onBackPressed() {

        FragmentManager fm = getSupportFragmentManager();
        Log.e("BACK STACK", "" + fm.getBackStackEntryCount());

        if(fm.getBackStackEntryCount() > 0) {
            fm.popBackStack();
        }
    }           
}

当我从 B 回压时,我可以回到 A,但我不能从 C 回到 B。谢谢你的帮助

编辑:我这样解决了:

公共类 FragmentTabActivity 扩展 FragmentActivity {

private Global globalClass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    globalClass = ((Global) getApplicationContext());

    setContentView(R.layout.activity_fragment_tab);

    getSupportFragmentManager().addOnBackStackChangedListener(
            new MainOnBackStackChangedListener());

}



private class MainOnBackStackChangedListener implements
        OnBackStackChangedListener {

    public void onBackStackChanged() {
        Log.e("Stack COUNT", "" + 
    getSupportFragmentManager().getBackStackEntryCount());
    }
}

@Override
public void onBackPressed() {

    try {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {


                int lastIndex=globalClass.historyList.size()-1;

                globalClass.historyList.remove(lastIndex);

                for (int i = 0; i <globalClass.counterForBackPress; i++) {

                    getSupportFragmentManager().popBackStack();
                }   

        } else {
                     FragmentTabActivity.this.finish();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

4

0 回答 0