I am doing some hands-on reading on AsyncTaskLoader so I can use the technique to load the contact list. The only time the code works is when I implement the callbacks from a class that extends Fragment
as in MyLoader extends Fragment implements LoaderCallbacks<ArrayList<Contact>>
. Is there another way? All I really need is the contact list (name, phone, thumbnail), to send to my backend. When, for example, I try to use Context
, since I can get that from any activity by simply doing (Context)this
, the code fails to even compile. By context I mean
context.getLoaderManager().initLoader(1, null, this);
//I already changed to Fragment so don't really remember the exact ".context" line.
//But someone who has done this will understand the snippet.
BTW: I am using multiple references. One is http://developer.android.com/reference/android/content/AsyncTaskLoader.html.
QUESTION (again): Can I use AsyncTaskLoader without Fragment or FragmentActivity?
THE CODE THAT WORKS WITH THE FRAGMENT:
package com.example.contactpreload.utils;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
public class LoadingContacts extends Fragment implements LoaderCallbacks<ArrayList<Contact>> {
ArrayList<Contact> loadedContacts;
static Fragment fragmentActivity;
public static LoadingContacts newInstance(int arg) {
LoadingContacts f = new LoadingContacts();
Bundle bundle = new Bundle();
bundle.putInt("index", arg);
f.setArguments(bundle);
fragmentActivity = new Fragment();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate()");
int mIndex = getArguments().getInt("index");
System.out.println(mIndex);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("onActivityCreated()");
getLoaderManager().initLoader(1, null, this);
}
@Override
public Loader<ArrayList<Contact>> onCreateLoader(int arg0, Bundle arg1) {
System.out.println("onCreateLoader()");
return new ContactsLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<ArrayList<Contact>> loader, ArrayList<Contact> data) {
loadedContacts = data;
System.out.println("AND THE CONTACTS ARE: ");
for (Contact c : loadedContacts) {
System.out.println("NAME: " + c.getName());
System.out.println("getPhoneNumberHome: " + c.getPhoneNumber());
}
}
@Override
public void onLoaderReset(Loader<ArrayList<Contact>> arg0) {
System.out.println("onLoaderReset()");
// TODO Auto-generated method stub
}
}
package com.example.contactpreload;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.example.contactpreload.utils.LoadingContacts;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadingContacts fragment = LoadingContacts.newInstance(1);
fragment.setRetainInstance(true);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MANIFEST:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />