I am trying to tidy up my code so that I can easily reuse elements without having to duplicate work/ code.
I have a contactpicker and code to process the result in my application that I want to put into its own external class so I can reuse it in multiple areas.
I have got stuck on one issue- in order to call the StartActivityforResult, I need to pass the activity to the external class- however I am not sure how to do that?
Here is the class I am using as the external contact picker:
public class ContactFinder {
private static final int CONTACT_PICKER_RESULT = 1001;
private Activity atv;
public ContactFinder(Activity atv) {
this.atv=atv;
}
public void startContactPicker()
{
Intent contactPickerIntent=new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
//Shows CONTACT_PICKER_RESULT cannot be resolved to a variable
atv.startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
}
so how would I call it from my main activity using the following:
ContactFinder cf=new ContactFinder(???));// how do I pass the activity to this external class
cf.startContactPicker();
Pass
thisasActivityin which you are creating this Object.Keep in mind to implement
onActivityResult()in yourActivityalso, to handle the result given by the startedActivity.Better to have a basic
Activityclass to which will implementonActivityResult()for yourContactFinderand extend everyActivity(in which you want to useContactFinder) from basicActvitiy