Passing information to another Activity

0

Good morning I'm trying to pass 2 data from one activity 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 Second Activity code:

 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;
    
asked by anonymous 13.11.2017 / 11:42

2 answers

0

You call the bundle correctly, but you need to pass the bundle through the "Intent".
Here's an 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 the other view, use the following code:

id_user = getIntent().getExtras().getString("id_do_item");
    
13.11.2017 / 11:47
0

Error

Your error is to be creating a new bundle and not passing it to activy, or you could use bundle of onCreate.

How to do

There are 2 ways:

Sem o Bundle (because its intent already has its own onCreate )

public void teste2(View v) {

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

Com 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 putExtras .

With bundle of onCreate , would be putExtra .

    
13.11.2017 / 11:52