Reputation: 16479
How can I send data to another Activity that did not startActivityForResult
?
I Override
the onActivityResult
method in order to perform logic as soon as a result returns from another Activity. Specifically, from my EditItemActivity
Process:
Within my MainActivity
,
when I click on an item in the ListView
, I am redirected to another Activity called ToDoDetailActivity
When I click the Edit
button I am redirector to another Activity called EditItemActivity"
As soon as I make my changes and press the Edit
button on the EditItemActivity
, I return back to the MainActivity
data = new Intent(EditItemActivity.this, MainActivity.class);
data.putExtra(EDITTEXT_VALUE, etValue);
setResult(123, data);
startActivity(data);
Within my MainActivity, I want to UPDATE the item I just edited with this logic within my onActivityResult
method. However, I don't see any logs in LogCat
meaning I do not believe this method is being used and therefore no logic is performed
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
return;
}
if (resultCode == 123) {
String editedItemValue = data.getStringExtra(EditItemActivity.EDITTEXT_VALUE);
todoItems.remove(editedPosition);
aToDoAdapter.insert(editedItemValue, editedPosition);
aToDoAdapter.notifyDataSetChanged();
}
}
Upvotes: 0
Views: 1370
Reputation: 5329
We can use BroadcastReceiver
or use database to store the result of your edit in EditItemActivity
then update that result in onResume method of MainActivity
Upvotes: 1
Reputation: 1398
In your code:
data = new Intent(EditItemActivity.this, MainActivity.class);
data.putExtra(EDITTEXT_VALUE, etValue);
setResult(123, data);
startActivity(data);
When you execute startActivity with MainActivity.class it is launching a new Activity so you are not going to find a result in "onActivityResult" method. It is because you are not returning to the main activity after finishing in a "child" activity.
What I have done in the past is use broadcast event to notify the "MainActivity" about updating the view.
Hope that helps.
Upvotes: 1