Search code examples
androidandroid-intentandroid-fragmentsandroid-fragmentactivityonactivityresult

onActivityResult not being called inside FragmentActivity that contains Android Map view fragment


I have a fragment activity that displays an Android V2 Map. Inside I also have a onActivityResult used to handle the intent Extras that needs to be passed from the calling activity

public class DisplayMap extends FragmentActivity implements LocationListener {

    @Override
    protected void onCreate(Bundle arg0) {
       // TODO Auto-generated method stub
       super.onCreate(arg0);

       setContentView(R.layout.map);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent){
       super.onActivityResult(requestCode, resultCode, intent);

       Log.v("TEST", "********************************************");
    }
} 

Here is the code form the activity that calls it.

Intent i = new Intent("com.example.DisplayMap");
setResult(RESULT_OK, i);
startActivityForResult(i, 2014);

But somehow the onActivityResult is not called inside.

Thanks in advance. Dennis


Solution

  • onActivityResult() needs to be in the calling activity, it retrieves the result, as the name suggests.

    To return a result from the called activity you'll need to use setResult() and finish that activity:

    called activity:

    Intent i = new Intent();
    setResult(RESULT_OK, i);
    finish();
    

    with RESULT_OK being passed as the resultCode parameter to onActivityResult() and i as intent

    calling activity:

    Intent i = new Intent("com.example.DisplayMap");
    startActivityForResult(i, REQUEST_CODE);
    

    and to receive the result:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    
        if(requestCode == REQUEST_CODE){
            if (resultCode == RESULT_OK){
                // RESULT OK, take the returned extras from intent and use them
            }
        }
    }