我的MainActivity正在调用第二个包含列表视图的活动PopupWindow。当用户单击列表视图时,我需要将该信息返回到第一个活动(MainActivity)。因此在MainActivity中,我有这两种方法。第一个方法调用第二个活动,第二个从第二个活动获取结果
//event listener for authors list menu (popup window)
authorListImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent popupWinIntent = new Intent(getBaseContext(), AuthorsPopup.class);
popupWinIntent.putExtra("allauthors", allAuthors);
startActivity(popupWinIntent);
}
});
//fetching result -- author from AuthorsPopup activity back
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
String author = data.getStringExtra("author");
Log.v("author ", author);
}
}
}
此方法在onCreate()方法之外。一些教程建议创建avobe方法,就像onActivityResul()一样,我假设这种情况将在onCreate()方法内部。我的是一个方法声明。如此执着地不执行。在我的第二项活动中。我有这个
//event listener for authros listview
authorListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> paren, View view, int position, long id) {
// Log.v("author: ", authorsList[position]);
String author = authorsList[position];
Intent returnResultIntent = new Intent(getBaseContext(), MainActivity.class);
returnResultIntent.putExtra("author", author);
setResult(RESULT_OK, returnResultIntent);
finish();
}
});
从第二个活动中取回数据的正确方法是什么?
qq_遁去的一_1
红糖糍粑
相关分类