Fragment lifecycle - which method is called upon show / hide?

前端 未结 11 898
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 08:31

I am using the following method to switch between Fragments (in my NavigationDrawer) by showing / hiding them.

protected void showFragment(int container, Fra         


        
相关标签:
11条回答
  • 2020-12-04 09:06

    of course you can @Override the following method to do so:

    @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            if (isVisibleToUser) {
                // Do your Work
            } else {
                // Do your Work
            }
        }
    
    0 讨论(0)
  • 2020-12-04 09:08

    Fragment in view pager behaviour is different with regular fragment container.

    Try this code:

        boolean mIsVisibleToUser;
    
        /**
         * is visible to user
         */
        public void show() {
            //do when show
        }
    
        /**
         * is invisible to user
         */
        public void hide() {
            //do when gone
        }
    
        @Override
        public void onResume() {
            super.onResume();
            if (!mIsVisibleToUser && getUserVisibleHint()) {
                mIsVisibleToUser = true;
                show();
            }
        }
    
        @Override
        public void onPause() {
            super.onPause();
            if (mIsVisibleToUser && getUserVisibleHint()) {
                mIsVisibleToUser = false;
                hide();
            }
        }
    
        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            if (isResumed()) {
                if (mIsVisibleToUser != isVisibleToUser) {
                    mIsVisibleToUser = isVisibleToUser;
                    if (isVisibleToUser) show();
                    else hide();
                }
            }
        }
    
        public boolean isVisibleToUser() {
            return mIsVisibleToUser;
        }
    
    0 讨论(0)
  • 2020-12-04 09:10

    setUserVisibleHint call before onCreateView. and you can't update any View inside setUserVisibleHint I use

    public void setMenuVisibility(final boolean visible)
    

    for visibility and onHiddenChanged() didn't call for the first time. it calls when the hidden state changes. because a fragment is visible by default. In order to achieve this method for the first time you have to call mFragmentTransaction.hide(oldFragment) then it will work

    Note

    if you want to use setUserVisible hint and update View Use this method

    0 讨论(0)
  • 2020-12-04 09:11

    You can use 'onCreateView'(or 'onActivityCreated') and 'onHiddenChanged'. Use 'onCreateView' for first show and use 'onHiddenChanged' for later. 'setMenuVisibility' is not called on transaction control.

    @Override
    public View OnCreateView() {
       // fragment will show first
    }
    
    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            // fragment will show 
        }
        else {
            // fragment will hide
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:12

    Of course you could override setUserVisibleHint or setMenuVisibility but if you need to access Context or Activity, they will be null in there! There is another method onStart which always has the context available at hand, but it will only get called once upon creation of fragment and if you start moving between your fragments in a pager you will see that it won't get called in second view and afterwards.

    So... what to do now?

    The workaround is quite easy, use onStart for the first visit and setMenuVisibility for later ones. Your code will probably look like below :

    Fragment class:

    public class MyFragmentClass{
        private boolean isCurrentVisible = false;
    ...
    
    @Override
    public void onStart() {
        super.onStart();
        if (isCurrentVisible)
            doSth();
    }
    
    @Override
    public void setMenuVisibility(boolean menuVisible){
        super.setMenuVisibility(menuVisible);
        this.isCurrentVisible = menuVisible;
        if(menuVisible && getContext() != null)
            doSth();
    }
    

    This way Context will always be available to doSth() method.

    0 讨论(0)
提交回复
热议问题