0

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:

Community
  • 1
  • 1
Diëgo
  • 103
  • 1
  • 9
  • 1
    instead of using getParent to communicate with the activity, you should use an interface and create a listener. – Martin Nov 24 '14 at 00:17
  • You mean to communicate with the previous activity? getParent is used in the second detail activity, but the else bracket of the if structure is never called... Would you still do the same? It's another way to achieve feedback, wanted to use the result. – Diëgo Nov 24 '14 at 09:25
  • 1
    Either way. Communication with parent fragments should always go through listeners like @Martin mentioned. – QuirijnGB Nov 25 '14 at 10:21

2 Answers2

0

**I also encountered the same problem of OnActivityResult() you have to change your implementation bc incase of Fragment the OnActivityResult() of the Host Activity is called not the fragment's **

Arun Salaria
  • 984
  • 6
  • 20
  • My problem isn't that the method isn't called, it is that the result code is not passed correctly... – Diëgo Nov 24 '14 at 09:21
0

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(); 
} 
QuirijnGB
  • 811
  • 1
  • 9
  • 18