gpt4 book ai didi

android - onActivityResult - resultCode 始终为 0

转载 作者:搜寻专家 更新时间:2023-11-01 08:29:21 25 4
gpt4 key购买 nike

我对 onActivityResult 有疑问,无论我在做什么,我都无法获得正确的 resultCode。

我知道有类似的问题,但最后他们没有帮助我,我无法解决

MainActivity: 将打开新 Activity Popup.class

的方法
public void openShopView(){
Intent intent = new Intent(this, Popup.class);
Bundle b = new Bundle();

b.putString("which", "ShopMain");
intent.putExtras(b);
startActivityForResult(intent, 1);
}

第二个 Activity: 将打开另一个 Activity Popup.class 的方法,只是布局不同

shop_c1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getIntent());
Bundle b = new Bundle();

b.putString("which", "ShopBuildings");
intent.putExtras(b);
startActivity(intent);
finish();
}
});

第三个 Activity: 还有一个方法应该设置结果并关闭 Activity

building2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("LOG_NEW: ", "" + getCurrentBuildingTable(1) + ", " + checkSlotTable(1));
if(getCurrentBuildingTable(1) && checkSlotTable(1) == -1) {
Intent returnIntent = getIntent();
returnIntent.putExtra("result", 1);
setResult(RESULT_OK, returnIntent);
finish();
}else if (checkSlotTable(1) == -1){
Log.i("LOG_NEW: ", "Building already exist");
}
else{
Log.i("LOG_NEW: ", "Not enough resources");
}
}
});

最后是来自 MainActivity 的 onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

Log.i("LOG_RES: ", "Checking.. " + requestCode + ", " + resultCode);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result = data.getStringExtra("result");
Log.i("LOG_RES: ", result);

}
}
}

无论我在做什么,我都无法启动 if(resultCode == RESULT_OK) 循环并且 resultCode 始终为 0..

感谢帮助

最佳答案

setResult 必须在 Second Activity 中调用,因为第二个 Activity 的 Intent 已在 startActivityForResult 中传递。

但是,您可以将第三个 Activity 的结果代码委托(delegate)给第二个 Activity ,然后再委托(delegate)给第三个。

将您的第二个 Activity 更改为如下内容:

shop_c1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getIntent());
Bundle b = new Bundle();

b.putString("which", "ShopBuildings");
intent.putExtras(b);
startActivityForResult(intent,1);
//Remove finish from here
}
});

然后在 Second Activity 中也添加这个

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1){
setResult(resultCode,data);
}

finish();
}

关于android - onActivityResult - resultCode 始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42032387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com