Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making getContentResolver() to work in class extending Fragment class



What I have in mind is to use two fragments, first one to show the contacts list and second one to show the details of contact which is being selected in the upper fragment.
The class that I'm using to extend the Fragment class does not allow me to use getContentResolver() method due to the context issue. Now I'm trying to fetch the contact in the same class which is extending Fragment class and use it to show in the list view and its details. After going through some older solution I found the way to create a function and pass Context to it as a parameter but the problem is I don't have to call it from any other class which is extending Activity. I want to do it from that same class extending Fragment.

How should I do this?

Any help will be highly appreciated.

like image 622
Syed Danish Ali Avatar asked Aug 06 '15 09:08

Syed Danish Ali


1 Answers

Try to add this code to a previous activity:

// a static variable to get a reference of our application context
public static Context contextOfApplication;
public static Context getContextOfApplication()
{
return contextOfApplication;
}

and in the same activity, in your onCreate method add this line:

 contextOfApplication = getApplicationContext();

In your fragment you can access this by using :

Context applicationContext = YourActivity.getContextOfApplication();
applicationContext.getContentResolver();
like image 96
Ahmed Abidi Avatar answered Oct 20 '22 00:10

Ahmed Abidi