当ListActivity完成的onActivityResult不会被调用 [英] 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.
在为了做到这一点,我创建 ListActivity
, GameListScreen
的一个子类,其中显示的记录和重写 onListItemClick()
如下:
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();
}
然后,为了发动我的活动,我有这个在我的的MainMenu
活动;一个的onClick
处理程序的按钮
:
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);
}
和得到的结果,也是在的MainMenu
类:
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
}
}
一切工作完全按照预期 - 在ListActivity开始,我可以看到我的记录,当我选择一个,onListItemClick正在运行 - 但的onActivityResult
是没有得到所谓的,我不知道为什么。在另一个项目中,我遵循相同的基本原则,并在那里工作。
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.
我上传我的项目的情况下,它帮助。我使用的是Android 2.2的测试,因为那是我的电话正在使用。
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.
推荐答案
也许这就是为什么
从的javadoc:
启动,而您希望的结果,当它完成了一个活动。当这个活动退出,你的onActivityResult()方法将与给定的请求code调用。 是一个负的请求code是与调用startActivity(意向)(该活动不会推出一个子活动)。
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).
这篇关于当ListActivity完成的onActivityResult不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!