1

Is it necessary to keep a reference to your spawned AlertDialog in your Activity and dismiss it in onDestroy() or could you just create it and forget it? It seems to be dismissed when your leave the Activity?

I suppose the AlertDialog holds a reference to the Activity Context normally, which might prevent the Activity from being garbage collected. What is the procedure to avoid possible memory leak here?

Does an AlertDialog belong to the Activity, i.e. does it follow the Activity lifecycle, being destroyed at the same time as the Activity and so on...?

JohnyTex
  • 3,323
  • 5
  • 29
  • 52
  • 2
    If you create an `AlertDialog` using the `Activity` context, it will be removed when the activity hits `onDestroy`. But if any other context is used which outlives the Activity like `applicationContext` then that is surely gonna cause some problems. – Darshan Jun 22 '22 at 07:50

1 Answers1

2

I think, if you spawned the alert Dialog using the activity context or activity scope... It will be automatically destroyed whenever the activity is destroyed. Maybe the problem arises when you want to spawn an Alert dialog from a reusable Utility class which doesn't have access to activity context, and ended up using Global Application context, then you need to manually destroy the dialog, since it may outlive the current activity, causing leak.

laggyPC
  • 19
  • 2