Weakhandler memory leak

2019-08-30 12:25发布

问题:

A warning says that handlers must be static or else leaks might occur but I do not know how to make it static, adding 'static' after 'private' does not work either

private  final WeakHandler<MainActivity> mGenericModelHandler = new WeakHandler<MainActivity>(this) {

        @Override
        public void handleMessage(Message msg, MainActivity thisActivity) {
            try {
                switch (msg.what) {
                case ModelBase.BUSY_STATE_CHANGED_NOTIFICATION:
                    if( mModel.error() != null ) {
                        mResultTextView.append("\n Task failed:\n" + mModel.error().getMessage() + "\n\n");
                        mResultScrollView.post(new Runnable() { public void run() { mResultScrollView.fullScroll(View.FOCUS_DOWN); } });

                    }
                    UpdateUI();
                    break;

                case MESSAGE_NOTIFICATION:
                    String message = (String)msg.obj;
                    mResultTextView.append(message);
                    mResultScrollView.post(new Runnable() { public void run() { mResultScrollView.fullScroll(View.FOCUS_DOWN); } });
                    break;

                default:
                    break;
                }
            } catch (Exception e) {
            }

        }
    };