Explain reasons why not to ...
Explain reasons why not to ...
Here are the main reasons why you should avoid using getApplicationContext()
in Android development:
The Application
context returned by getApplicationContext()
has a longer lifecycle than an Activity
or Fragment
context. It exists for the entire duration of the app process, even if all activities are destroyed. This can lead to memory leaks if you hold onto references to the Application
context in places where it outlives the intended scope.
Unlike Activity
and Fragment
, the Application
class does not have lifecycle callbacks like onCreate()
, onStart()
, onResume()
, etc. This makes it harder to properly manage resources and subscriptions tied to the Application
context.
When the device configuration changes (e.g. screen rotation, language change), Activity
and Fragment
instances are recreated, but the Application
instance persists. T...
expert
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào