I have one class LogBeerActivity that extends AppCompatActivity and has these two methods:
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);
}
and
@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);
}
}
}
The other class AddBreweryActivity also extends AppCompatActivity and it has this method called when its form with user input is submitted:
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();
}
}
}
However, the onActivityResult method never gets called, even though the flow between the two activities happens as expected.
EDIT: I've made a discovery. If I comment out the lines of code that do the HTTP request to my server (From the line starting with URL url = ... until and including the line starting with thread.start(); then it works. Is it something to do with starting the new thread that messes things up? In fact, it's enough to just comment out the line that reads thread.start(); and it works (except the http request isn't made, of course).
from Why do I not get the result back from my startActivityForResult?
No comments:
Post a Comment