setResult(RESULT_OK)不成功的原因。

本文探讨了在Android应用中从ActivityA启动ActivityB时遇到的一个问题:当ActivityB设置为singleInstance模式时,无法通过常规方式从A获取到B设置的结果。文章详细解释了这一现象的原因,并提供了解决思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ActivityA 启动Activity B。

A:

activity.startActivityForResult(intent, requestCode)


B:

setResult(RESULT_OK);

finish();


A:onActivityResult

发现得到的resultCode并不是RESULT_OK。 原因是在B中我们设置了android:launchMode="singleInstance", 这样的话,A,B将不会在同一个栈中,导致setResult(RESULT_OK);失败。

public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ActivityResultLauncher<Intent> deviceListLauncher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); // 初始化设备列表的启动器 deviceListLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { Intent data = result.getData(); // 处理设备列表返回的数据 if (data != null) { String deviceAddress = data.getStringExtra(“device_address”); tv_blueTooth.setText(deviceAddress); addressmac = deviceAddress; initOPSClient(addressmac); cardConnSta = true; //setButtonEnable(); Log.d(TAG, “选择的设备地址:” + addressmac); } } }); } private void openBlueTh() { Log.d(TAG, “初始化蓝牙!”); if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); bluetoothEnableLauncher.launch(enableIntent); // 使用新API } Intent serverIntent2 = new Intent(this, DeviceListActivity.class); //startActivityForResult(serverIntent2, SETTING_BT); deviceListLauncher.launch(serverIntent2); // 使用新API }public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ActivityResultLauncher<Intent> deviceListLauncher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); // 初始化设备列表的启动器 deviceListLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { Intent data = result.getData(); // 处理设备列表返回的数据 if (data != null) { String deviceAddress = data.getStringExtra(“device_address”); tv_blueTooth.setText(deviceAddress); addressmac = deviceAddress; initOPSClient(addressmac); cardConnSta = true; //setButtonEnable(); Log.d(TAG, “选择的设备地址:” + addressmac); } } }); } private void openBlueTh() { Log.d(TAG, “初始化蓝牙!”); if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); bluetoothEnableLauncher.launch(enableIntent); // 使用新API } Intent serverIntent2 = new Intent(this, DeviceListActivity.class); //startActivityForResult(serverIntent2, SETTING_BT); deviceListLauncher.launch(serverIntent2); // 使用新API }deviceListLauncher.launch(serverIntent2)调用后闪退,没有结果回调,debug发现在DeviceListActivity中setResult(Activity.RESULT_OK, intent); finish();后出现闪退,请分析如何解决
03-25
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ActivityResultLauncher<Intent> deviceListLauncher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); // 初始化设备列表的启动器 deviceListLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { Intent data = result.getData(); // 处理设备列表返回的数据 if (data != null) { String deviceAddress = data.getStringExtra("device_address"); tv_blueTooth.setText(deviceAddress); addressmac = deviceAddress; initOPSClient(addressmac); cardConnSta = true; //setButtonEnable(); Log.d(TAG, "选择的设备地址:" + addressmac); } } }); } private void openBlueTh() { Log.d(TAG, "初始化蓝牙!"); if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); bluetoothEnableLauncher.launch(enableIntent); // 使用新API } Intent serverIntent2 = new Intent(this, DeviceListActivity.class); //startActivityForResult(serverIntent2, SETTING_BT); deviceListLauncher.launch(serverIntent2); // 使用新API }public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ActivityResultLauncher<Intent> deviceListLauncher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); // 初始化设备列表的启动器 deviceListLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { Intent data = result.getData(); // 处理设备列表返回的数据 if (data != null) { String deviceAddress = data.getStringExtra("device_address"); tv_blueTooth.setText(deviceAddress); addressmac = deviceAddress; initOPSClient(addressmac); cardConnSta = true; //setButtonEnable(); Log.d(TAG, "选择的设备地址:" + addressmac); } } }); } private void openBlueTh() { Log.d(TAG, "初始化蓝牙!"); if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); bluetoothEnableLauncher.launch(enableIntent); // 使用新API } Intent serverIntent2 = new Intent(this, DeviceListActivity.class); //startActivityForResult(serverIntent2, SETTING_BT); deviceListLauncher.launch(serverIntent2); // 使用新API }deviceListLauncher.launch(serverIntent2)调用后闪退,没有结果回调
03-25
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值