为什么我无法从我的 startActivityForResult 中获取结果?



我有一个类LogBeerActivity扩展AppCompatActivity,并且具有以下两种方法:

public void gotoAddBrewery(View view) {
String breweryNameEntered = breweryTextView.getText().toString();
Intent intent = new Intent(this, AddBreweryActivity.class);
intent.putExtra(Constants.EXTRAS_ID_BREWERY_NAME, breweryNameEntered);
startActivityForResult(intent, ADD_BREWERY_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG_LOG_BEER_ACTIVITY, "Got request result code: " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case ADD_BREWERY_REQUEST_CODE:
if (resultCode == RESULT_OK)
{
String breweryName = data.getStringExtra(AddBreweryActivity.BREWERY_NAME_CODE);
breweryTextView.setText(breweryName);
}
}
}

另一个类AddBreweryActivity也扩展了AppCompatActivity,当提交带有用户输入的表单时,它有这个方法:

public void submitBrewery(View view) {
EditText breweryLocationInput = findViewById(R.id.input_brewery_location);
EditText breweryCountryInput = findViewById(R.id.input_brewery_country);
if (breweryNameInput == null || breweryLocationInput == null || breweryCountryInput == null) {
Log.e(TAG_ADD_BREWERY_ACTIVITY, "One of the inputs is null!");
return;
} else {
String enteredBreweryName = breweryNameInput.getText().toString().trim();
String enteredBreweryLocation = breweryLocationInput.getText().toString().trim();
String enteredBreweryCountry = breweryCountryInput.getText().toString().trim();
Log.d(TAG_ADD_BREWERY_ACTIVITY, "submitBeer clicked. Brewery: " + enteredBreweryName + " from: " + enteredBreweryLocation + " in: " + enteredBreweryCountry);
if (!enteredBreweryName.isEmpty() && !enteredBreweryLocation.isEmpty() && !enteredBreweryCountry.isEmpty()) {
NewBrewery newBrewery = new NewBrewery(enteredBreweryName, enteredBreweryLocation, enteredBreweryCountry);
URL url = HttpHelper.getUrl(Constants.URL_BASE + Constants.URL_BREWERIES_PATH);
Thread thread = new Thread(() -> {
try {
HttpHelper.makeRequest(url, newBrewery, this);
} catch (IOException e) {
Log.e(TAG_ADD_BREWERY_ACTIVITY, "Error trying to submit new brewery: ", e);
return;
} catch (HttpHelper.UserNotLoggedInException e) {
Log.e(TAG_ADD_BREWERY_ACTIVITY, "Error trying to submit new brewery: ", e);
return;
}
});
thread.start();
Intent resultIndent = new Intent();
resultIndent.putExtra(BREWERY_NAME_CODE, newBrewery.getName());
setResult(Activity.RESULT_OK, resultIndent);
finish();
}
}
}

但是,永远不会调用onActivityResult方法,即使两个活动之间的流按预期发生也是如此。

编辑:我有一个发现。如果我注释掉向我的服务器发出 HTTP 请求的代码行(从以URL url = ...开头的行到包括以thread.start();开头的行,那么它就可以工作了。这与启动搞砸事情的新线程有关吗?事实上,只需注释掉读取thread.start();并且它起作用的行就足够了(当然,除了没有发出 http 请求(。

虽然实现看起来很好,但感觉它没有进入 IF 条件。尝试注释掉你的代码,只执行setResult,看看它是否在调用onActivityResult 例如

public void submitBrewery(View view) {
Log.e(TAG,"Submitting setResult");
Intent resultIndent = new Intent();
resultIndent.putExtra(BREWERY_NAME_CODE, "BREWERY_NAME");
setResult(Activity.RESULT_OK, resultIndent);
finish();
}

虽然我知道您的问题已解决,但我很好奇如果您尝试以下操作会发生什么:

Thread thread = new Thread(() -> {
try {
HttpHelper.makeRequest(url, newBrewery, this);
} catch (IOException e) {
Log.e(TAG_ADD_BREWERY_ACTIVITY, "Error trying to submit new brewery: ", e);
return;
} catch (HttpHelper.UserNotLoggedInException e) {
Log.e(TAG_ADD_BREWERY_ACTIVITY, "Error trying to submit new brewery: ", e);
return;
}
AddBreweryActivity.this.runOnUiThread(new Runnable() {
public void run() {
Intent resultIndent = new Intent();
resultIndent.putExtra(BREWERY_NAME_CODE, newBrewery.getName());
setResult(Activity.RESULT_OK, resultIndent);
finish();
}
});
});
thread.start();

对不起人们,但事实证明我只是一个白痴......

在我的 HttpHelper.makeRequest 方法中,我在某个时候(可能是一年前,当我最后一次从事该项目时(进行了"黑客">

LoginActivity.startLoginActivity(context);

这反过来又做到了:

Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent);

这当然会破坏整个结果的回报,但这并不明显,因为它最终会让你在同一页面上。黑客攻击的原因是,这是一种在开机自检后从服务器取回更新数据的简单方法......如果我在 HttpHelper 中注释掉该行,一切正常(除了我必须更新本地数据以包含新啤酒厂,但这是一项不同的任务(。

我不确定我是否应该删除整个问题,因为它不太可能对其他人有用......?

您正在 AddBreweryActivity 上创建一个新的 Intent 对象。不调用活动结果是正常的。 您应该获取通知结果的当前意图,哪个活动发送了意向对象。 如果更改此代码行

Intent resultIndent = new Intent();

有了这个,

Intent resultIndent = getIntent();

它将通知onActivityResultgetIntent((来自LogBeerActivity的活动。

相关内容

  • 没有找到相关文章

最新更新