pass result form current activity to first activity when go back android
I have two classes NoteEdit and NoteView, in NoteView class I press edit button it direct me to NoteEidt class, suppose I want to pass my updated body information to NoteView, and the previous body information should have been updated in NoteView interface, but it still remain the same, this is part of my code, please help, thanks!
NoteEdit class:
Intent resultIntent = new Intent(NoteEdit.this, Note开发者_如何转开发View.class);
String body = mBodyText.getText().toString()
resultIntent.putExtra(body, true);
NoteView class
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DIARY_EDIT:
Intent i = new Intent(NoteView.this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
startActivityForResult(i, ACTIVITY_VIEWERS);
break;
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode) {
case (ACTIVITY_VIEWERS)
: {
if (resultCode == Activity.RESULT_OK)
{
Bundle extras = getIntent().getExtras();
newText =extras.getString(NotesDbAdapter.KEY_BODY);
// TODO Update your TextView.
}
break;
}
}
}
now use this code.
NoteEdit class:
Intent resultIntent = new Intent(NoteEdit.this, NoteView.class);
String body = mBodyText.getText().toString()
resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body);
setResult(RESULT_OK,resultIntent);
NoteView class:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DIARY_EDIT:
Intent i = new Intent(NoteView.this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
startActivityForResult(i, ACTIVITY_VIEWERS);
break;
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode) {
case ACTIVITY_VIEWERS
: {
if (resultCode == RESULT_OK)
{
Bundle extras = intent().getExtras();
newText =extras.getString(NotesDbAdapter.KEY_BODY);
// TODO Update your TextView.
}
break;
}
}
}
use setResult(Activity.RESULT_OK, resultIntent)
in NodeEdit
and see http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Edit
also you should use resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body);
in NodeEdit
to pass String value body
to another activity. and get it by using extras.getString(NotesDbAdapter.KEY_BODY);
try something like this.
Intent resultIntent = getIntent();
String body = mBodyText.getText().toString()
resultIntent.putExtra(body, true);
setResult(RESULT_OK,resultIntent);
finish();
dont forget to put a call to finish()
.
NoteEdit class:
//write this code when edit finished
Intent resultIntent = new Intent(getIntent()); // Note this line is important!
String body = mBodyText.getText().toString()
resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body);
setResult(RESULT_OK,resultIntent);
finish();
NoteView class:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DIARY_EDIT:
Intent i = new Intent(NoteView.this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
startActivityForResult(i, ACTIVITY_VIEWERS);
break;
}
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); switch(requestCode) { case ACTIVITY_VIEWERS: { if (resultCode == RESULT_OK) { Bundle extras = intent().getExtras(); newText =extras.getString(NotesDbAdapter.KEY_BODY); // TODO Update your TextView. } break; } } }
精彩评论