android what to use instead of onRestart() in a fragment

前端 未结 5 1316
既然无缘
既然无缘 2021-01-20 15:34

I\'m dealing with .setVisibility() of a view, inside my main fragment at app start. So what I want is that the view is invisible on app sta

5条回答
  •  猫巷女王i
    2021-01-20 16:06

    Fragments don't have onRestart(). It's only for Activities.

    See the lifecycle of fragments below

    I suppose you're looking for onResume() instead


    Use a boolean flag to check whether or not you're returning to the Fragment:

    private boolean firstVisit;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        //other stuff
        firstVisit = true;
    }
    
    @Override
    public void onResume() {
        //other stuff
        if (firstVisit) {
            //do stuff for first visit only
    
            firstVisit = false;
        }
    }
    

提交回复
热议问题