need for onActivityResult() when result is already obtained by onListItemClick()
I have a main class which extends Activit开发者_JAVA技巧y which should spawn a ListActivity and obtain the selection made. So I have an onListItemClick() in the class which extends ListActivity which accepts the selection via getItemAtPosition.
This ListActivity is started by startActivityForResult.
Now, since I have already obtained the result in onListItemClick, why do I need onActivityResult() ?? what does it do?
and where does the intent come in?
You are using startActivity for result if you want to return the result of the selection from the list to your MainActivity. StartActivity for result enables you to pass the value that was selected from the list back to the main Activity.
At the moment that the value is handled in the onListItemClick the value is only known to your spawned ListActivity. To pass it back you would use setResult(int, Intent) like this:
Intent resultIntent = new Intent;
resultIntent.putExtra(CONSTANT_FOR_RESULT, selection);
setResult(CONSTANT_FOR_SUCCESS, resultIntent)
finish();
This will have your app return to the first activity and as explained in the android documentation onActivityResult will be called. Now you can extract the selection out of the intent and use it in your main activity.
You only need this mechanism if you want to pass a selection made in Activity B back to Activity A.
精彩评论