我在活动中的片段中有一个列表,该列表会启动详细信息,其中包含一个包含所选对象信息的片段。
如果选择了其中一个操作项,则活动结果为OK,如果再次选中,则结果将切换回CANCELED。我只想在用户导航回列表时传递此结果。 我已经消除了几个问题,并且我到达了listfragment中的onActivityResult函数,但结果代码不是我传递的那个。 在调试时,我还注意到listactivity中的onActivityResult函数永远不会被触发。
ListActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("LA", "onActivityResult: " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
}
ListFragment:
/**
* Launch detail activity
*/
public class OnObjectClickListener implements OnItemClickListener {
public OnObjectClickListener() {
super();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("OnObjectClickListener", "Selected an object");
ObjectAdapter adapter = (ObjectAdapter) _objList.getAdapter();
Object o = adapter.getItem(position);
Intent i = new Intent(adapter.getContext(), DetailActivity.class);
i.putExtra(DetailActivity.ARG_OBJCODE, o.getCode());
startActivityForResult(i, DetailActivity.ARG_FOLLOW_OBJ_CHANGE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("LF", "Activity result received");
if (requestCode == DetailActivity.ARG_FOLLOW_OBJ_CHANGE
&& resultCode == Activity.RESULT_OK
&& data != null
&& data.hasExtra(DetailActivity.ARG_OBJCODE)){
// Update object in the list: following indicator has changed
String objcode = data.getStringExtra(DetailActivity.ARG_OBJCODE);
ObjectAdapter oa = (ObjectAdapter) _objList.getAdapter();
for (Object o : oa.getObjects()){
// Toggle following state in the game
if (o.getCode().equals(objcode)){
o.setFollowing(!o.isFollowing());
oa.notifyDataSetChanged();
Log.i("LF", "Edited dataset");
break;
}
}
Log.i("LF", "Result processed");
} else {
super.onActivityResult(requestCode, resultCode, data);
Log.i("LF", "Result not recognized");
}
}
行动项详细活动:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
DetailFragment df = (DetailFragment) getFragmentManager().findFragmentByTag(_objCode);
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_follow_obj:
if (df != null){
df.followObj(true);
configResult();
}
break;
case R.id.action_unfollow_obj:
if (df != null){
df.followObj(false);
configResult();
}
break;
}
return super.onOptionsItemSelected(item);
}
/** Sets the result of this activity (did the following parameter change, or not?)
* @return nothing */
private void configResult() {
// switch follow state monitor: statechange is true upon uneven changes
// meaning the state is not really changed if the game is first followed and subsequently unfollowed
_followStateChanged = !_followStateChanged;
Log.i("DA", "State changed: " + _followStateChanged);
}
这就是我尝试将结果返回到上一个listfragment的方式:
@Override
public void onBackPressed() {
super.onBackPressed();
// config result
Intent i = new Intent();
i.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
i.putExtra(DetailActivity.ARG_OBJCODE, _objCode);
int result = _followStateChanged ? Activity.RESULT_OK : Activity.RESULT_CANCELED;
if (getParent() == null){
setResult(result, i);
} else {
getParent().setResult(result, i);
}
this.finish();
Log.i("DA", "Back pressed, intent passed and set as a result");
}
... getParent()始终为null
resultCode总是0,有没有人知道我哪里出错了? 我已经咨询了这些答案,应用并测试了它们,得到了进一步但尚无解决方案:
答案 0 :(得分:0)
**我也遇到了同样的问题OnActivityResult()你必须改变你的实现bc包含Fragment,Host Activity的OnActivityResult()被调用而不是片段的**
答案 1 :(得分:0)
你应该在方法结束时调用super。不是开始。
@Override
public void onBackPressed() {
// config result
Intent i = new Intent();
i.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
i.putExtra(DetailActivity.ARG_OBJCODE, _objCode);
int result = _followStateChanged ? Activity.RESULT_OK : Activity.RESULT_CANCELED;
if (getParent() == null){
setResult(result, i);
} else {
getParent().setResult(result, i);
}
this.finish();
Log.i("DA", "Back pressed, intent passed and set as a result");
super.onBackPressed();
}