Passing Activity Context to constructors to use internally - is this bad

后端 未结 3 1243
情话喂你
情话喂你 2021-02-04 01:52

Is it bad practice to pass the Context to a constructor and save it as a private variable for internal use? The other option is to pass the Context as a parameter to methods th

3条回答
  •  太阳男子
    2021-02-04 02:18

    In the main application (which launches), declare a variable appContext: "public static Context appContext;" Then, in the onCreate() method for this main application, assign: "appContext = this;" Because appContext is public, any other class in this package can then use appContext to track down the XML resources. Is this any better (from memory standpoint)?

    Even better might be to declare the Resources object as public static in the main application and then use it elsewhere, since resources is all you need.

提交回复
热议问题