Reputation: 3333
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).
Upvotes: 1
Views: 341
Reputation: 10547
though the implementation seems fine, It feels like that it's not going into the IF Condition. Try commenting out your code and only execute the setResult and see if it's calling the onActivityResult or not e.g.
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();
}
Though I know your problem has been solved, I'm curious what happens if you try the following:
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();
Upvotes: 1
Reputation: 3333
I'm sorry people, but it turned out that I was just being an idiot...
In my HttpHelper.makeRequest method I'd put in a "hack" at some point (maybe a year ago, when I was last working on the project) that did:
LoginActivity.startLoginActivity(context);
Which in turn did:
Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent);
Which of course would derail the whole returning with the result, but it wouldn't be obvious, as it would end you up on the same page. The reason for the hack was that it was a simple way to get the updated data back from the server after the POST... if I comment out that line in the HttpHelper, it all works fine (except I have to update the local data to contain the new brewery, but that's a different task).
I'm not sure if I should delete this whole question, as it's unlikely useful to anyone else...?
Upvotes: 1
Reputation: 151
You are creating a new Intent object at AddBreweryActivity. It is normal to not calling the activity result. You should get the current intent for the notify on result which activity send the intent object. If you change this code line
Intent resultIndent = new Intent();
with this one,
Intent resultIndent = getIntent();
it's going to notify onActivityResult
of activity where getIntent() is coming from which is LogBeerActivity.
Upvotes: 0