Can a custom View know that onPause has been called?

后端 未结 6 2160
南方客
南方客 2021-02-03 18:52

I have a custom View that runs a Thread operation which sits around making calls to the interwebs periodically. I would like to know if there\'s a way for me to not have to kil

6条回答
  •  臣服心动
    2021-02-03 18:57

    Yes you can using below code,

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == View.VISIBLE) //onResume called
        else // onPause() called
    }
    
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) //onresume() called
        else // onPause() called
    }
    
    @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            // onDestroy() called
    }
    
    @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            // onCreate() called
    }
    

提交回复
热议问题