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

前端 未结 5 1234
闹比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 04:43

    It should be safe. Also the following note from the API docs could be relevant to you:

    There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

提交回复
热议问题