Reputation:
I'm working on to get contacts from phone, for this purpose I'm using LoaderCallbacks<Cursor>
I create a new class with FetchContacts
name and implement loaderManager
. Now I want when ever I create object of that class loaderManager automatically initialize.
FetchContacts
public class FetchContacts implements LoaderManager.LoaderCallbacks<Cursor> {
private Context context;
FetchContacts(Context ctx){
context = ctx;
getLoaderManager().initLoader(0, null, this); // Error: Undefined method
}
// Reset of code like override methods.
MainActivity
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FetchContacts fetchContacts = new FetchContacts(this);
}
}
I know the reason of error because FetchContacts
is not extend from Activity class. Is it necessary to extend it from Activity class or is there and other method to call it from MainActivity ?
Upvotes: 1
Views: 3435
Reputation: 1513
Pass LoaderManager
as an Argument as @Mike said.
FetchContacts
public class FetchContacts implements LoaderManager.LoaderCallbacks<Cursor> {
private Context context;
FetchContacts(Context ctx, LoaderManager loaderManager){
context = ctx;
loaderManager.initLoader(0, null, this);
}
// Reset of code like override methods.
MainActivity
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FetchContacts fetchContacts = new FetchContacts(this, getLoaderManager());
}
}
Upvotes: 6