I have MainActivity and I´m using startActivityForResult in it to open another activity in which I´m working with some data in list. I can edit these data and delete too. After clicking on back button of my phone I want to send this edited list back to main activity. Where should I create that intent with that new list?Is there any method which I can override for that back button pressed?
            Asked
            
        
        
            Active
            
        
            Viewed 1,376 times
        
    0
            
            
        - 
                    yes. Override onBackPressed() on your second activity and start an intent that goes to the main activity with extras. – kha Apr 25 '15 at 14:30
- 
                    Yeah that is what i was looking for. I probably missed that when i was searching in override methods! Thank you! – previ Apr 25 '15 at 14:34
2 Answers
3
            
            
        In your MainActivity call the AnotherActivity using startActivityForResult() method:
Intent i = new Intent(this, AnotherActivity.class);
startActivityForResult(i, 1);
If you need, in your AnotherActivity set the data which you want to return back to MainActivity.
Intent returnIntent = new Intent();
returnIntent.putExtra("mData", object);
setResult(RESULT_OK, returnIntent);
finish();
In MainActivity you have to Override/Implement the onActivityResult() method in this way:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result=data.getStringExtra("mData");
        }
        if (resultCode == RESULT_CANCELED) {
            // do something if there is no result
        }
    }
}
See more:
 
    
    
        Community
        
- 1
- 1
 
    
    
        antoniodvr
        
- 1,259
- 1
- 14
- 15
0
            You can override onBackPressed and pass the data back to the original Activity. But in order to pass it like this, the list must implement Parcelable
@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent();
    Bundle data = new Bundle();
    data.putParcelableArrayList("someKey", listThatImplementsParcelable);
    intent.putExtras(data);
    setResult(RESULT_OK, intent);
    finish();
}
 
    
    
        Bojan Kseneman
        
- 15,488
- 2
- 54
- 59
 
    