Jose M.
Jose M.

Reputation: 219

Get object on onActivityResult

I make a new activity that create a Bluetooth device object. When I need return this using onActivityResult() method, I have a problem.

Child activity

Intent intent = new Intent();
            intent.putExtra("BluetoothDevice", DeviceArrayList.get(arg2));
            setResult(Activity.RESULT_OK, intent);
            finish();

DeviceArrayList.get(arg2) is the device Object.

Parent activity

 BluetoothDevice btDevice;
...

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (requestCode == 1) {
                if(resultCode == Activity.RESULT_OK){

                    btDevice = data.getExtras("BluetoothDevice");

                }
                if (resultCode == Activity.RESULT_CANCELED) {
                    Toast.makeText(this, "Resultado cancelado", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }//onActivityResult

Who I can get the object in btDevice?

Upvotes: 0

Views: 2139

Answers (2)

pkgorver
pkgorver

Reputation: 11

make BluetoothDevice class serializable and in onActivityResult cast it like that

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){

                btDevice =(BluetoothDevice)data.getSerializableExtra("BluetoothDevice");

            }
            if (resultCode == Activity.RESULT_CANCELED) {
                Toast.makeText(this, "Resultado cancelado", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }//onActivityResult

Upvotes: 1

avin
avin

Reputation: 469

Make your bluetooth device class serializabale or parceable. And then you can pass it to the second activity using intent:

intent.putExtra("bluetooth", myBluetoothDeviceObject);

And you can get the same in your second activity using:

intent.getParcelableExtra("bluetooth")

I prefer parceable over serializable. Please look at the android docs for more info: http://developer.android.com/reference/android/os/Parcelable.html

Upvotes: 2

Related Questions