Using Application context everywhere?

后端 未结 9 995
难免孤独
难免孤独 2020-11-22 01:52

In an Android app, is there anything wrong with the following approach:

public class MyApp extends android.app.Application {

    private static MyApp instan         


        
9条回答
  •  时光取名叫无心
    2020-11-22 02:09

    It is a good approach. I use it myself as well. I would only suggest to override onCreate to set the singleton instead of using a constructor.

    And since you mentioned SQLiteOpenHelper: In onCreate () you can open the database as well.

    Personally I think the documentation got it wrong in saying that There is normally no need to subclass Application. I think the opposite is true: You should always subclass Application.

提交回复
热议问题