android handler正确写法

        为避免内存泄漏,正确写法应该是写个内部静态类+弱引用:

    private static class MyHandler extends Handler {

        private WeakReference activityWeakReference;

        public MyHandler(LoginActivity activity) {
            activityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            LoginActivity activity = activityWeakReference.get();
            if (activity != null) {
               
            }
        }
    }

 

你可能感兴趣的:(android)