How to call method from another class without passing context?

旧巷老猫 提交于 2019-11-29 12:24:40

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()

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!