1

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 ?

  • You could pass the `LoaderManager` as an argument in the `FetchContacts` constructor, but I'm not really sure why you want to do it this way. http://stackoverflow.com/a/13433085 – Mike M. Feb 19 '17 at 06:33
  • Because I want to get the contacts whenever I need it. If I'm not able to call `initLoader()` then this class is useless. Is there anyother way to do this. I'm new in android. Passing `loaderManager` is a good way ? –  Feb 19 '17 at 06:44
  • You don't need a `Loader` to get Contacts. `Loader`s are mainly for delivering content updates, changes, etc. to a UI component, like an `Activity`. You can always query the Contacts Provider yourself wherever you have access to a `Context`. http://stackoverflow.com/q/12562151 To specifically answer your last question, no, using a `LoaderManager` like that is not really a good way to do this. – Mike M. Feb 19 '17 at 06:49
  • But I find something that `cursorLoader` is good `Note: All the examples in this lesson use a CursorLoader to retrieve data from the Contacts Provider. A CursorLoader runs its query on a thread that's separate from the UI thread. This ensures that the query doesn't slow down UI response times and cause a poor user experience` This is from android developer https://developer.android.com/training/contacts-provider/retrieve-names.html#Permissions –  Feb 19 '17 at 07:08
  • Yeah, I figured that's where you got it from, when I went searching for that example. As I said, `Loader`s are for delivering results to an `Activity` or `Fragment`. If those are the only places you need the results, then there's no need for your separate class; just implement the necessary `Loader` setup in whichever `Activity`/`Fragment` you need. If by "I want to get the contacts whenever I need it", you mean you're trying to use that one `Loader` or its results somewhere else, you're gonna have a bad time. – Mike M. Feb 19 '17 at 07:15

1 Answers1

6

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());
       }
     }
Azeem Haider
  • 1,443
  • 4
  • 23
  • 41
  • 1
    Note - this should be used with caution as the LoaderManager class holds a reference to the activity. If you're not careful, this can lead to memory leaks. – Chris Oct 11 '17 at 18:25