简体   繁体   中英

Pass strings to another Activity in Android

Maybe someone can help me. I would like to pass two strings from to an other activity. This is the code where i generate the strings:

private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null;
        String name = null;

        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();

        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact name
        int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        // Set the value to the textviews
        textView1.setText(name);
        textView2.setText(phoneNo);
    } catch (Exception e) {
        e.printStackTrace();
    }

I would like to pass phoneNo and Name to my MainActivity

I tried to do a SharedPreference and load DefaultSharedPreference in my MainAcitivity without any Result

Add this in your code

Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("key", keyId);
startActivity(intent);

and get it as from second activity

Bundle extras = getIntent().getExtras();
String value;
if (extras != null) {
 value = extras.getString("key");
}

in current activity add this:

Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("PHONE", phoneNo);
intent.putExtra("NAME", Name);
startActivity(intent);

in main activity add this:

Intent intent = getIntent();
String phonNo = intent.getStringExtra("PHONE");
String Name = intent.getStringExtra("Name");

You can use Intent

// to pass data from one activity to other via Intent

Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.putExtra("phone", phoneNo);
intent.putExtra("name", name);
startActivity(intent);

//To receive data in other activity

Intent intent = getIntent();
String phonNo = intent.getStringExtra("phone");
String name = intent.getStringExtra("name");

Guessing from your comments that you want to pass the strings once you're closing your ContactActivity without opening again the MainActivity (not adding it to the stack).

You can start an Activity and wait for a result, so in your Main Activity change

   startActivity(intent);

for

   startActivityForResult(i, REQUEST_CODE);  

Then, in your ContactActivity you can set your result as the following:

   intent = new Intent();
   intent.putExtra("PHONE", phoneNo);
   intent.putExtra("NAME", Name);
   setResult(RESULT_OK, intent);

And in onActivityResult you can implement onActivityResult to retrieve the data

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
       if(requestCode == REQUEST_CODE){

            if(resultcode == RESULT_OK){
                 String phoneNo = data.getStringExtra("PHONE");
                 String name = data.getStringExtra("NAME");
                 //do whatever you want
            }else{
                 //do something else
            }
       }
   } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM