I am trying to keep my code tidy and move most of the functionality to external classes where possible so I can re-use it easily.
I have a contact picker which I am currently using and want to move it into its own class to aid reusability. Is it possible and if so how do I move the bottom two methods out into their own class?
public class mainDashView extends Activity {
public static final int PICK_CONTACT = 1;
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
showContactSelector();
}
// How do I move the bottom two Methods into an external class?
private void showContactSelector() {
Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactIntent, PICK_CONTACT);
}
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
}
like so:
You need to pass the calling Activity for some of the methods to work. For example within your Activity class you can call
startActivityForResult(contactIntent, PICK_CONTACT);directly but that is only a shortened version ofthis.startActivityForResult(contactIntent, PICK_CONTACT);. In a different class you need to replace the invisiblethisby a reference to the class.