2

I am using Handler in my splash screen for delaying redirection to the next activity as follows..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entrance);
        screenTimeOut();
    }

    private void screenTimeOut() {
          /* New Handler to start the next screen
         * and close this Entrance after some seconds.*/
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                initTracker();
                /* Create an Intent that will start the Next-Activity. */
                checkLoginStatus();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

And in another activity am passing context to a class and holding the context inside that as follows on button click ..

 private Tools tools;

 tools = new Tools(DetailsScreen.this, true);

Tools

    private Context _context;
    private Fragment _fragment;
    private Activity activity;
    private String filePath = null;
    private String camImagePath = null;

    public Tools() {
    }

    public Tools(Context _context, boolean flag) {
        this._context = _context;
        this.activity = (Activity) _context;
        initPath();
        if (flag) {
            createImageFile();
        }
    }

Is any one of these will be a reason for leakage ?

And how about using handler as follows..

 private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entrance);
        screenTimeOut();
    }

 private void screenTimeOut() {
          /* New Handler to start the next screen
         * and close this Entrance after some seconds.*/
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initTracker();
                /* Create an Intent that will start the Next-Activity. */
                checkLoginStatus();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }
Stella
  • 1,817
  • 6
  • 30
  • 55
  • Will `tools` remain even after `DetailsScreen` finished? – shhp Oct 26 '15 at 06:29
  • http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html pls check this http://stackoverflow.com/questions/10864853/when-exactly-is-it-leak-safe-to-use-anonymous-inner-classes and this too – Vaisakh Oct 26 '15 at 06:32
  • Nop..Tools declared as private non-static field..@shhp – Stella Oct 26 '15 at 06:32

1 Answers1

2
  1. Handler and Runnable should not be used in anonymous fashion.
  2. You should avoid passing the whole activity as a parameter to other classes. If context is all that you want use activity.getContext().

More info here:
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html http://www.androiddesignpatterns.com/2013/04/activitys-threads-memory-leaks.html

Henry
  • 17,490
  • 7
  • 63
  • 98
  • Thank u for the response..Can u elaborate it both with an example of the right usage ..? Anything wrong in my code ? – Stella Oct 26 '15 at 07:07
  • The links I have sent you will tell to elaborately with example on the `handler` and `runnable` and how they cause the memory leak. I could not give you a better example. – Henry Oct 26 '15 at 07:23
  • I realized how to use handlers..but confused in second one about passing context. – Stella Oct 26 '15 at 07:26
  • It's a bad idea to pass `Activity` to another class. `Activity` holds reference to all your `Views` and when you pass it to another class, you should be very careful about the lifetime of that class. So it's a bad idea to pass the whole activity. More info here: http://stackoverflow.com/a/33162612/4747587 – Henry Oct 26 '15 at 09:04
  • If i should pass the Context to another class means how can i it without causing to a memory leak ? and what about this http://stackoverflow.com/questions/3346080/android-references-to-a-context-and-memory-leaks?rq=1 ? am following the same way right ? – Stella Oct 26 '15 at 09:44
  • You should make sure the object of the class that has your context dies along with the Activity. This means that, the reference to that object should be present only in your Activity and nothing else. Yes, that link explains it all. – Henry Oct 26 '15 at 09:51
  • I have edited my query..if am following the updated way it can be possible to avoid memory leak right ?..or i should make runnable static ? – Stella Oct 26 '15 at 10:35
  • Your `Runnable` is still anonymous. Try to avoid `anonymous` creation of objects completely, may it be `handle`, or `runnable` or `onClickListener` – Henry Oct 26 '15 at 11:52
  • That's why i had put `mHandler.removeCallbacksAndMessages(null);` in onDistroy to prevent memory leak removing the callbacks..it won't work u mean ? – Stella Oct 27 '15 at 04:23