Question

I'm experimenting with creating a custom Android Home Screen. I've used the sample Home Screen application and have adapted it. Something I would like to do, is open the contact picker from a button in the Home Screen and use the contact that the user chose in a next action. I've stumbled on the same problem that is mentioned in this question.

How can I work around this so that the home screen stays "singleInstance" and I can also call startActivityForResult()?

Is the contacts picker an activity that I can subclass (I've searched but can't find any) so that I can use the solution that David Wasser proposes in the aforementioned question?

Was it helpful?

Solution

I've found an elegant solution after all:

My main activity launches an intermediate, invisible activity that has android:theme="@android:style/Theme.NoDisplay"

This intermediate activity calls the contact picker in its onCreate

Intent phoneContactIntent = new Intent(Intent.ACTION_PICK, 
    ContactsContract.Contacts.CONTENT_URI);
// Show user only contacts w/ phone numbers
phoneContactIntent.setType(Phone.CONTENT_TYPE); 
startActivityForResult(phoneContactIntent, CHOOSE_CONTACT_TO_CALL);

Then, in onActivityResult, it creates a new intent for the main application, with the data that the contact picker returned.

    switch (requestCode) {
    case (CHOOSE_CONTACT_TO_CALL):
        if (resultCode == Activity.RESULT_OK) {
            Intent resultIntent = new Intent(this, Home.class);
            resultIntent.putExtras(data);

            Uri contactData = data.getData();
            if (contactData != null)
            {
                resultIntent.setData(contactData);
            }
            startActivity(resultIntent);
        }
    }
    finish();

and in my Home class, in onCreate I call getIntent() and inspect the data in the intent that launched the main activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top