back button in listView causes crash
I have an editText and button. Push the button, it launches a listView activity. Click on an item 开发者_JAVA技巧in the listView, it closes that activity and sets the editText to the item that was clicked on.
But if you get to the listView and hit the back button with clicking an item, it crashes. I think I need to do something in onPause but not sure if that's the best way to go about it.
listView, when item clicked...
Intent intent = new Intent();
Bundle b = new Bundle();
b.putString("TEXT", ((TextView) view).getText().toString());
intent.putExtras(b);
setResult(SUCCESS_RETURN_CODE, intent);
finish();
onActivityResult....
Bundle b = data.getExtras();
mEditCategory.setText(b.getString("TEXT"));
You are getting an exception: data
is null
because your activity did not return a result.
By default, the back key will end the activity without setting a result. Check the resultCode
parameter to onActivityResult
to see whether the sub-activity returned because the list item was clicked:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == SUCCESS_RETURN_CODE) {
// TODO handle data here from list item click
}
}
The resultCode
will be RESULT_CANCELED
if the activity explicitly returned that, didn't return any result, or crashed during its operation (see docs for Activity).
精彩评论