0

There have been many successful answers to this question, but there's still something I'm not understanding or that I'm doing incorrectly. One answer is implementing onActivityResult in the host Activity, but I guess I don't know which one that is or I'm following it wrong.

And no, I'm not calling getActivity.startActivityForResult(), just startActivityForResult().

Depending on a selection made in FirstActivity, one or more other selections are possible, created using Fragments. One Fragment is ButtonFragment, which when selected, starts OptionsActivity, like so:

public class ButtonFragment extends Fragment
{
  .....

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode != Activity.RESULT_CANCELED)
    {
       if(requestCode == OPTIONS_REQUEST_CODE)
       {
          Bundle extras = data.getExtras();
          if (extras != null) {
             String selection = (String) extras.get("optionsSelection");
             data.setText(selection);
          }
       }
    }
  }

  private void openOptionsActivity()
  {
     Intent intent = new Intent(getActivity(), OptionsActivity.class);
     intent.putExtra("optionsArray", options);
     startActivityForResult(intent, OPTIONS_REQUEST_CODE);
  }
}

This fragment is being added in BaseActivity:

public class BaseActivity extends FragmentActivity
{
  .....

    fm.beginTransaction().add(
            R.id.FragmentContainer, frag).commitAllowingStateLoss();
    fm.executePendingTransactions();

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
     super.onActivityResult(requestCode, resultCode, data);
     finish();
  }
}

Inside OptionsActivity:

public class OptionsActivity extends BaseSpinnerActivity
{
  .....

  private class OptionsAdapter extends BaseSpinnerAdapter
  {
    public OptionsAdapter(Context context, Object[] values)
    {
       super(context, values);
    }

    protected void fillRow(ViewHolder holder, int position)
    {
       String option = options[position];

       if(option != null) {
          holder.text.setText(option);
       }
    }

    protected void onRowSelect(View v)
    {
       Intent returnIntent = new Intent();
       String optionSelection = (String)v.getTag();
       returnIntent.putExtra("optionSelected", optionSelection);
       setResult(RESULT_OK, returnIntent);
       finish();
    }
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    super.onActivityResult(requestCode, resultCode, data);
    finish();
  }
}

The same thing is done inside BaseSpinnerActivity, which extends from ListActivity. (I know, should use FragmentActivity, but I haven't had a chance to convert it yet.)

None of these onActivityResult()'s are being called.

What am I missing or doing wrong? Any help is appreciated!

craned
  • 2,991
  • 2
  • 34
  • 38
  • in same Activity in which activity you are adding `ButtonFragment` override `onActivityResult` then this method call – ρяσѕρєя K Sep 22 '14 at 18:07
  • That's what I thought too, but it's not stopping there either. See my added info above. – craned Sep 22 '14 at 18:17
  • What do you mean 'this method call'? – craned Sep 22 '14 at 18:22
  • you call `startActicityForResult()` from ButtonFragment so override its `onActivityResult` – pskink Sep 22 '14 at 18:33
  • Yes, I failed to include that code earlier. It's there now. It still doesn't stop. – craned Sep 22 '14 at 18:38
  • if you are overriding FragmentActivity.onActivityResult make sure you call super implementation, then Fragment.onActivityResult will be called nicely – pskink Sep 22 '14 at 18:49
  • @pskink Near as I can tell, that's what I'm doing, but it's not working. – craned Sep 22 '14 at 18:52
  • i cant tell you why its not working as i cannot see all of your code, but i am sure if you call startActivityForResult() in your Fragment and if you override that Fragment's onActivityResult (and only there) it **muust** work – pskink Sep 22 '14 at 19:19
  • That's not what others have said: like [here](http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment/#24303360) – craned Sep 22 '14 at 20:00

0 Answers0