Is it safe to save the app context to a static variable in Android?

前端 未结 5 1241
闹比i
闹比i 2020-12-03 04:39

I know that usage of static variables on Android is quite risky, especially if you reference them to activities. However, if I have a class that extends Application (let\'s

5条回答
  •  借酒劲吻你
    2020-12-03 05:03

    Interesting comment popped up from Studio when I was tidying up nasty static contexts:

    "This is a leak (and also breaks Instant Run)."

    So with the launch of Instant Run, we have the case where Android developers are not planning on saving static variables. Whilst instant run is not (yet) on my agenda, it is useful to know that there is a specific example where it is not only bad practice, but the use case where it is wrong is identified.

提交回复
热议问题