开发者

onActivityResult not being called when ListActivity finishes

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开发者_如何学C == 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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜