Câu hỏi phỏng vấn Android
Câu hỏi

What happens if the user navigates away or closes the app while I still...

Câu trả lời

Here's how I would answer that Android interview question:

If the user navigates away or closes the app while you still have a reference to the Activity that was just closed in your AsyncTask, it can lead to a memory leak and potentially cause your app to crash.

When an Activity is destroyed, its reference should be properly cleared to allow the garbage collector to reclaim its memory. However, if you have a long-running AsyncTask that still holds a reference to the closed Activity, the Activity instance will remain in memory, even though it is no longer visible or accessible to the user.

This situation can cause a memory leak, which means that the memory occupied by the Activity instance is not being released, leading to a gradual increase in memory usage over time. As more Activities are closed while their references are still held by AsyncTasks, the memory usage will continue to grow, potentially causing your app to crash due to insufficient memory.

To avoid this issue, you should properly handle the lifecycle of your AsyncTask and ensure that it releases any references to the Activity when it is no longer needed. Here are a few strategies you can use:

  1. Cancel the AsyncTask when the Activity is destroyed: In the Activity's onDestroy()...
expert

expert

Gợi ý câu hỏi phỏng vấn

middle

Describe different types of Services in Android

junior

What is ADB and what is it used for?

entry

What is the difference between Bitmap and Drawable in Android?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào