Make your handler a static class.
A warning is a warning. You can turn off the warning, but its useful information
Here is a list of Lint Check
http://tools.android.com/tips/lint-checks
Quote from source @
http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html
Avoid non-static inner classes in activity if you don't control their life cycle, use a static inner class and loosely reference activity inside.
The solution to this problem is to use a static inner class with WeakReference for the outer class, as is done in ViewRoot and its inner class W, for example.
Also check out this discussion in the Android development team. Check Romain Guy's Solution
https://groups.google.com/forum/#!topic/android-developers/1aPZXZG6kWk
An example from the solution of Romain Guy from the link
class OuterClass { class InnerClass { private final WeakReference<OuterClass> mTarget; InnerClass(OuterClass target) { mTarget = new WeakReference<OuterClass>(target); } void doSomething() { OuterClass target = mTarget.get(); if (target != null) target.do(); }
Edit:
Example:
public class MainActivity extends Activity { LinearLayout ll; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ll = new LinearLayout(this); new HandlerClass(this); } private static class HandlerClass extends Handler{ private final WeakReference<MainActivity> mTarget; public HandlerClass(MainActivity context) { mTarget = new WeakReference<MainActivity>((MainActivity) context); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); MainActivity target = mTarget.get(); if (target != null) if(msg.what==1){ target.ll.removeAllViews();
Correct me if the above is incorrect or has some problems.
You can also check out this blog by Alex Lockwood.
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html