tags:

views:

66

answers:

1

I have a main class which extends Activity 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?

A: 

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.

Janusz
Thank you. How do I make fields accessible across a package? Currently, even if they are declared public i'm not able to access the fields from another class in the same package.
Namratha
The code snippet that you gave, where should it be placed? in Activity B but where?
Namratha
after you got the result of the selection. I don't know where that is maybe the OnListItemClicked method.
Janusz
OK. Thanks a lot Janusz!
Namratha
When you have a list activity and in onListItemClick() you need to obtain the selected item and then match it against various options, what is the efficient way to do it?switch case cannot be used since I want to match Strings.Is a very long if-else if ladder the only way to do it?
Namratha
I would recommend to ask further questions as a new question on this page. If you post me a link in the comments on this question i will have a look at the new question.
Janusz
here you arehttp://stackoverflow.com/questions/3186120/onlistitemclick-how-to-match-efficiently-result-string-with-various-options
Namratha