2

In a small app I'm working on, I need to be able to select a record from a database table.

In order to do that, I've created a subclass of ListActivity, GameListScreen, which displays the records, and overridden onListItemClick() as follows:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Game g = (Game)getListView().getItemAtPosition(position);
    Intent intent = new Intent();
    intent.putExtra("id", g.getId());
    setResult(RESULT_OK, intent);
    finish();
}

Then, in order to launch my activity, I have this in my MainMenu activity; an onClick handler for a Button:

public void openGameClick(View view) {
    Intent intent = new Intent(this, GameListScreen.class);
    startActivityForResult(intent, -1);
}

and to get the result, also in the MainMenu class:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        // result is handled here
    }
}

Everything works exactly as expected - the ListActivity starts, I can see my records, and when I select one, onListItemClick is being run - but onActivityResult is not getting called, and I have no idea why. In another project, I follow the same basic principle, and it works there.

What am I missing here? I'm sure it's a simple mistake, but I can't spot it.

I've uploaded my project in case it helps. I'm using Android 2.2 to test, since that's what my phone is using.

Michael Madsen
  • 54,231
  • 8
  • 72
  • 83

1 Answers1

9

Perhaps this is why

From the javadocs:

Launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode. Using a negative requestCode is the same as calling startActivity(Intent) (the activity is not launched as a sub-activity).

NickT
  • 23,844
  • 11
  • 78
  • 121
  • Indeed, setting it to 0 did the trick. I'm not sure why, though, because looking at my other project, I use -1 there - but maybe I forgot to commit my changes to that one from my other computer. Oh well - thanks a bunch for the help, now I can finally move on. – Michael Madsen Mar 13 '11 at 14:02
  • I had the same problem and was passing a negative value in as my requestCode. Changing to a positive value for requestCode immediately did the trick. – Darren Hicks Jan 15 '12 at 05:01