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

前端 未结 7 2028
梦谈多话
梦谈多话 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条回答
  • 2021-02-04 01:49

    Try to use the replace method instead add on the FragmentTransaction. This work for me:

    FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container, fragment);
    ft.addToBackStack(null);
    ft.commit();
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-02-04 01:58

    As others have said already, onResume() is only called when the activity itself is resumed, so that isn't going to help at all.

    You need to check if you're adding a new fragment, or replacing an existing fragment in your fragment transaction:

    • If you replace() a previous fragment, that previous fragment will be recreated from scratch when you go back to it, so onCreateView() will be called again, and you can update your toolbar title there. You probably do already.

    • If you add() a new fragment, the previous fragment is still there, only not visible. When you go back to it, it's up to you to get the last entry from the back stack (use getBackStackEntryCount() and getBackStackEntryAt() in the fragment manager), get the corresponding Fragment object (use findFragmentByTag() in the fragment manager), cast that Fragment to some base class that all your fragments will inherit from, and call a custom method, e.g. onVisible(), on that fragment. The default implementation of onVisible() in your base class does nothing. Override in each fragment to update toolbar title, FAB, and anything else as required. I'm also calling onVisible() from onResume() to avoid code duplication.

    0 讨论(0)
  • 2021-02-04 02:02

    I use this way, add this block code in your fragment

    requireActivity().supportFragmentManager.addOnBackStackChangedListener {
                val fm = requireActivity().supportFragmentManager
                fm?.let {
                    if (it.backStackEntryCount == YOUR_FRAGMENT_BACK_STACK_INDEX) {
                        // your fragment visible
                    }
                }
            }
    
    0 讨论(0)
  • 2021-02-04 02:08

    Try to change title on onCreateView() of Fragment.

    0 讨论(0)
  • 2021-02-04 02:12

    you can use this method of fragment :

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }
    
    0 讨论(0)
提交回复
热议问题