How to call method from another class without passing context?

前端 未结 2 1965
一个人的身影
一个人的身影 2020-12-19 18:52

I am currently trying to call a method from a utility class that will reference a new cursor created for this utility method. Unfortunately, my new class will not let me cr

2条回答
  •  不知归路
    2020-12-19 19:27

    Create a class that extends Application for your project (you have to declare it in the Manifest too), in the Application make a

    private static MyApplication app
    

    in the onCreate() of it assign it to the field

    app = this;
    

    and make a

    public static MyApplication get()
    

    in it. When you need a Context you can use a

    MyApplication.get()
    

提交回复
热议问题