onActivityResult() method not being called in an activity that extends SherlockMapActivity

Ingrid Cooper picture Ingrid Cooper · Sep 18, 2012 · Viewed 9k times · Source

I have an activity "SearchActivity" which extends SherlockActivity. In this activity I have an options menu which has a search bar and a "Go" button. The data that is entered in the search bar has to be passed to the previous activity "NavigationActivity" which extends SherlockMapActivity.

This is a part of my code of SearchActivity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.go:
           Intent intent = new Intent();
           intent.putExtra("SearchText", enteredText);
           setResult(200, intent);
           startActivityForResult(intent, 100);
           finish();
           break;

    }
    return true;
}

This is the onActivityResult() method in NavigationActivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    System.out.println("In onActivityResult method");
    if(data != null && resultCode == 200 && requestCode == 100) {
        String text = data.getStringExtra("SearchText");
        System.out.println("The data is: " + text);
        Toast.makeText(this, "The text entered in the search bar is: " + text, Toast.LENGTH_LONG).show();
    }       
}

The problem is that the onActivityResult() method is never getting called in the NavigationActivity. On pressing the "Go" button I'm able to navigate from the SearchActivity to the NavigationActivity but not able to get the get the data in the NavigationActivity. Could you please help me in this aspect?

Thank you.

Answer

Hitesh Jain picture Hitesh Jain · Sep 18, 2012

Please see the sample code below and modify your code accordingly.

Navigation Activity

public class NavigationActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigationActivity);

    Button btnTest = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
                    {
          Intent intent = new Intent(Launcher.this,Activity2.class);
          startActivityForResult(intent, 100);
        }

    });

}

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

    if(resultCode ==  200)
    {

        String enteredText = "no action defined for this requestcode :"+resultCode;
        if(requestCode == 100)
        {
            enteredText = (String)data.getStringExtra("SearchText");

        }
        Toast.makeText(Launcher.this,enteredText,Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(Launcher.this,"some exception occurred",Toast.LENGTH_SHORT).show();
    }
}
}

SEARCH ACTIVITY

public class SearchActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    Button btnGO = (Button)findViewById(R.id.buttonGo);
    btnGO .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            EditText edtSearchBar = (EditText )findViewById(R.id.tvTest);

               intent.putExtra("SearchText", edtSearchBar .getText().toString());

            setResult(200,intent);
            finish();

        }
    });
}
}