1.

I have to design one application where if update us going on Screen should ON. What approach I should follow here to make Screen On.

Answer»

In Android, it is very normal to found memory leak scenario as many TASKS are running in the background thread. Many TIME if ACTIVITY or fragment who start background thread using Async task is KILLED, Still the background thread will be alive.

We can handle this problem by calling the method removeCallbacksAndMessages on the handler in the onDestroy method of the activity.

If we cancel the task, we will not cause a memory leak. We can also solve that problem by constructing the new JAVA file with a context.getApplicationContext() instead of normal getContext / this (Activity). Then it will not be tied to the activity but to the application. We won't be able to access the dialog in onPostExecute(). Instead, we can use a callback to a listener if we want. 

Please find below code for more detail:

 @Override     protected void onDestroy() {         super.onDestroy();         //This resolves the memory leak by removing the handler references.         mHandler.removeCallbacksAndMessages(null);


Discussion

No Comment Found

Related InterviewSolutions