I have a simple form where a user can add, edit, and delete people from a list. When a user has chosen to edit a person it executes startActivityForResult so it can make the appropriate changes and refresh the list once the edit is complete. If the user clicks the back button from the edit screen a force close error appears.
I believe it has something to do with the system expecting a result, and I'm not trapping it properly. How would I trap this error?
Here is the onActivityResult code currently in place:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
switch(requestCode) {
case ACTIVITY_CREATE:
String person = extras.getString("person");
mDbHelper.addPerson(person);
fillData();
break;
case ACTIVITY_EDIT:
Long rowId = extras.getLong("_id");
if (rowId != null) {
String editPerson = extras.getString("person");
mDbHelper.updatePerson(rowId, editPerson);
}
fillData();
break;
}
}
Thank you for any help.
You want to wrap your Activity in an if statement and check resultCode before accessing the intent's bundle:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
// Add this line:
if (resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
switch(requestCode) {
case ACTIVITY_CREATE:
break;
case ACTIVITY_EDIT:
break;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With