How to call method from another class without passing context?

前端 未结 2 1961
一个人的身影
一个人的身影 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()
    
    0 讨论(0)
  • 2020-12-19 19:41

    A few hints on Context:

    1. Get Context in you View via getContext() and pass it to getContentResolver(context).

    2. Use application-context approach as described here by @apps.

    3. Don't store context inside Activity or Views. This leads to memory leaks.

    0 讨论(0)
提交回复
热议问题