android memory management in activity lifecycle

前端 未结 5 1560
小鲜肉
小鲜肉 2020-12-25 13:47

My question is a little bit complicated.

I want to understand how the app treats resources (especially images for backgrounds, buttons, etc.) when Activity is starte

5条回答
  •  情书的邮戳
    2020-12-25 14:35

    Well before replying to your questions, I have certain facts to discuss.

    1. As per the life cycle of the Activity, if we call finish() then the onStop() called and finally the onDestroy() which eligibles that Activity for Garbage Collection and remove from the Activity Stack of Android.

    2. Android is maintaining the Drawable cache for the Activity designing and displaying on the screen. So if you disable the drawable cache on the Activity onCreate().

    So the best practice is to disable drawable cache on the onCreate like this:

     LinearLayout v = (LinearLayout) findViewById(R.id.mainLayout);
            v.setDrawingCacheEnabled(false);
    

    and to call finish(); on the onPause();

提交回复
热议问题