Why FragmentManager's getBackStackEntryCount() return zero?

后端 未结 5 1552
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 13:01
private static void changeFragment(Fragment f, boolean init) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.info_content, f,f.getC         


        
5条回答
  •  清歌不尽
    2020-12-10 13:19

    It might be too late to answer this question. Hope this answer will help someone anyway.

    Mostly it depends on where you are actually calling getBackStackEntryCount() method. In my case, I was calling this method after calling super.onBackPressed(). The moment this method was got called, there was no fragment in back stack. That's why I was always receiving 0.

    Right way of calling the method in onBackPressed() :

       @Override
    public void onBackPressed() {
        try {
            int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
            Log.d("class", "items in backstack " + backStackEntryCount);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onBackPressed();
    }
    

提交回复
热议问题