Fragment's onResume() not called when popped from backstack

前端 未结 7 2031
梦谈多话
梦谈多话 2021-02-04 01:17

Hi I am developing android application in which I am using I am using single Activity and 3 fragments. So consider I have 3 fragments A B C. When I switch from A to

7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 01:54

    onResume() of the Fragment is called only when the Activity is resumed. So this wont help you. Even I'm facing similar issue right now. You can implement OnBackStackChangedListener and get the fragment name in the top of the stack and set the ActionBar title based on that.

    private FragmentManager.OnBackStackChangedListener getListener()
    {
        FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener()
        {
            public void onBackStackChanged()
            {
                FragmentManager manager = getFragmentManager();
    
                if (manager != null)
                {
                    if(manager.getBackStackEntryCount() >= 1){
                        String topOnStack = manager.getBackStackEntryAt(manager.getBackStackEntryCount()-1).getName();
                        Log.i("TOP ON BACK STACK",topOnStack);
                    }
                    }
                }
        };
    
        return result;
    }
    

提交回复
热议问题