Jun Yang
Jun Yang

Reputation: 21

Is it leak safe to just use weak reference of Activity in non-static inner classes?

Use reference of Activity in non-static inner classes such as Handler will case memory leak, but if I never use Activity reference directly and just use the weak reference, is any problem?

Upvotes: 0

Views: 238

Answers (1)

itzcoatl90
itzcoatl90

Reputation: 11

No it is not... The weak references solves just half the problem... The inner instance keep a reference even if you do nothing (if it's not static)... the unintended reference will keep 'alive' the activity to the garbage collector... So, no, it's not leak safe. Well this is on cases where this inner class runs on a background task or if it's not private and somebody else on the outside can keep a reference to the inner.

Upvotes: 1

Related Questions