I've got a list in a fragment in an activity which launches a detailactivity which contains a fragment with info on the selected object.
If one of the action items is selected, the result of the activity is OK, if it is selected again, the result switches back to CANCELED. I only want to pass this result when the user navigates back to the list. I've eliminated several issues already, and I arrive in the onActivityResult function in the listfragment, but the resultcode is not the one I passed. Upon debugging, I also noticed the onActivityResult function in the listactivity is never triggered.
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");
}
}
Action items detail activity:
@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);
}
And this is how I attempt to get the result back to the previous 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() is always null
The resultCode is always 0, does anyone have an idea where I went wrong? I've already consulted these answers, applied and tested them, got further but no solution yet:
You should call super at the end of the method. Not the beginning.
@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();
}