Passing information to another Activity

Asked

Viewed 566 times

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?

  • @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 with startActivityForResult(Intent intent, int requestCode) and return from it to the first Activity calling the method setResult (int resultCode, Intent data)&#Here’s a good example: https://www.javatpoint.com/android-startactivityforresult-example

2 answers

1

You call Bundle correctly, but still have to pass the Bundle by "Intent".
Following example (With Bundle):

        Bundle bundle = new Bundle();
        bundle.putString("id_do_item", "seu_dado");
        Intent intent = new Intent(context, MinhaOutraActivity.class);
        intent.putExtra(bundle);
        startActivity(intent);

To access in the other view, use the following code:

id_user = getIntent().getExtras().getString("id_do_item");
  • Matheus! But here’s the thing! I give Finish on an Activity! if you repair I open an Activity to get the contact number (User’s Schedule) that the user select, soon after I finalize and return to the first Activity

  • It is in vdd when I posted that you had not made the edit yet.... I will review your code again and edit my reply

  • right Matheus, I’m on hold

0

Error

Your mistake is creating a new bundle and not pass it to activy, or could use the bundle of onCreate.


How to do

There are 2 ways:

Sem o Bundle (because his intent already has one on onCreate)

public void teste2(View v) {

    Intent i = new Intent(this,Teste.class);
    i.putExtra("site","site2.com");
    startActivity(i);
}

Com a new Bundle:

public void teste(View v) {

    Intent i = new Intent(this,Teste.class);

    Bundle bd = new Bundle();
    bd.putString("site","google.com");

    i.putExtras(bd);
    startActivity(i);
}

With new bundle, using the putExtras.

With the bundle of onCreate, would be the putExtra.

  • Raoni! But here’s the thing! I give Finish in an Activity! if you repair I open an Activity to get the contact number (User’s Schedule) that the user select, soon after I finalize and return to the first Activity

  • Then you have treat in the other Activity. Each Activity passes the data to the next.

  • I don’t quite understand

  • Why don’t you call startActivity(intent); after giving finish() in the first Activity?

  • Or save in some sharedpreferences your data then, and recover in the next activity

  • Every time you come out of one activity for another, you need to pass the data. So if you are on activity 1, passed the data to the activity 2, and wants to return them to the activity 1, then also need to do the same process in activity 2. Or like Lmaker said, use sharedpreferences, or other type of armazenamento depending on what you need.

  • @Lmaker got it! however he is sending the message twice to the user

  • @Lmaker if I call to open the Activity it will give nullpointer no?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.