Tony
Tony

Reputation: 73

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, NoteView.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;    
            }    
        }  


    }   

Upvotes: 2

Views: 715

Answers (4)

Taras Mazepa
Taras Mazepa

Reputation: 642

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; } } }

Upvotes: 0

Adil Soomro
Adil Soomro

Reputation: 37729

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().

Upvotes: 0

ilango j
ilango j

Reputation: 6037

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;    
        }    
    }  


}   

Upvotes: 1

Taras Mazepa
Taras Mazepa

Reputation: 642

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);

Upvotes: 0

Related Questions