1
Good morning I’m trying to pass 2 data from one Active to another but I’m not getting below the relevant code in the first Activity :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String phoneNumber="";
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
Bundle bundle = new Bundle();
bundle.putString(name, String.valueOf(name));
bundle.putString(phoneNumber, String.valueOf(phoneNumber));
}
//mainActivity.onBackPressed();
// Toast.makeText(mainactivity, "go go go", Toast.LENGTH_SHORT).show();
// tvname.setText("Name: "+name);
// tvphone.setText("Phone: "+phoneNumber);
// Log.d("curs", name + " num" + phoneNumber);
}
c.close();
}
finish();
}
Below is the relevant code of Segunda Activity :
intent = getIntent();
Bundle getBundle = null;
getBundle = intent.getExtras();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
String phone = bundle.getString("phoneNumber");
messageContact = name+'\n'+ phone;
Diego, did any answer solve your problem? Would there be something else we can explain?
– rbz
@Diego, actually, what you need, as far as I could see, and start Activity correctly, since you collect the data in the method
onActivityResult
and this only works if you call your second Activity withstartActivityForResult(Intent intent, int requestCode)
and return from it to the first Activity calling the methodsetResult (int resultCode, Intent data)
&#Here’s a good example: https://www.javatpoint.com/android-startactivityforresult-example– Armando Marques Sobrinho