You can do it this way
Note: this code is in KOTLIN
JAVA
version is also below it
Declare variable
var launchData: ActivityResultLauncher<Intent>? = null
instead of onActivityResult
, in onCreate
mention before opening new Activity
launchData =
registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{
if (it.resultCode == Activity.RESULT_OK) {
if (it.data != null) {
it.data.let { obj ->
var detail = obj?.getStringExtra("details")
// or whatever your result keys are
}
}
}
}
instead of startActivityForResult
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE).also {
launchData!!.launch(it)
}
JAVA
Variable Declaration
ActivityResultLauncher<Intent> launchData = null;
in onCreate
launchData= registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
data.getStringExtra("details");
}
}
});
result callback
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
launchData.launch(enableBtIntent);