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
代码报错: java.lang.UnsupportedOperationException: remove,代码如下:package com.example.myapplication; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Arrays; import java.util.List; public class DetailActivity extends AppCompatActivity { String flag=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); TextView name=(TextView)findViewById(R.id.name); TextView phone=(TextView) findViewById(R.id.phone); Button common_used=(Button)findViewById(R.id.common_used); Button back=(Button)findViewById(R.id.back); Intent intent=getIntent(); String user_name="姓名:"+intent.getStringExtra("name"); String user_phone="手机号码:"+intent.getStringExtra("number"); String position=intent.getStringExtra("position"); name.setText(user_name); phone.setText(user_phone); common_used.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //存入 SharedPreferences sharedPreferences=getSharedPreferences("people",MODE_PRIVATE); SharedPreferences.Editor editor=sharedPreferences.edit(); editor.putString("Isstar",""); String FirstPosition=sharedPreferences.getString("Isstar",""); List<String> result = Arrays.asList(FirstPosition.split(",")); if(result.contains(position)){//点击前位置是星 flag="unused"; while (result.contains(position)) { result.remove(position); } String newIsstar=String.join(",",result); editor.putString("Isstar",newIsstar); Toast.makeText(DetailActivity.this,"删除此重点联系人", Toast.LENGTH_SHORT).show(); Toast.makeText(DetailActivity.this,newIsstar, Toast.LENGTH_SHORT).show(); // Log.i("新号","星号有:"+newIsstar); } else{ editor.putString("Isstar",FirstPosition+","+position); Toast.makeText(DetailActivity.this,"添加为重点联系人", Toast.LENGTH_SHORT).show(); flag="used"; String ceshi=sharedPreferences.getString("Isstar",""); Toast.makeText(DetailActivity.this,ceshi, Toast.LENGTH_SHORT).show(); } editor.commit(); } }); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent_back=new Intent(DetailActivity.this,PhoneActivity.class); intent_back.putExtra("position",position); // Toast.makeText(DetailActivity.this,String.valueOf(position), Toast.LENGTH_SHORT).show(); intent_back.putExtra("flag",flag); // setResult(RESULT_OK,intent_back); // finish(); startActivity(intent_back); } }); } }
最新发布
07-13
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值