1

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.

harshit
  • 3,788
  • 3
  • 31
  • 54
Ingrid Cooper
  • 1,191
  • 3
  • 16
  • 27
  • http://forum.processing.org/topic/startactivityforresult-and-onactivityresult-not-returning-any-value – Nermeen Sep 18 '12 at 07:05

3 Answers3

1

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();

        }
    });
}
}
Hitesh Jain
  • 408
  • 2
  • 5
  • Thank you for your reply. I tried removing finish() but it still didn't work. Its navigating back to the NavigationActivity but the onActivityResult() is still not getting called in the NavigationActivity. – Ingrid Cooper Sep 18 '12 at 07:09
  • Thank you for your reply. I tried that and it worked. I'm able to get the data that I entered in the SearchActivity to be displayed in the NavigationActivity. – Ingrid Cooper Sep 18 '12 at 10:28
1

Doesn't it need to be the other way around?

From my understanding you need to call startActivityForResult(..) from your NavigationActivity and not from the SearchActivity.

In NavigationActivity:

Intent intent= new Intent(context, SearchActivity.class);
startActivityForResult(intent, 200);

And then in the SearchActivity call

setResult(..);
finish();

to get the result back to the calling activity and trigger your onActivityResult(..)method.

See this related answer.

Community
  • 1
  • 1
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • Thank you for your reply. I'll try calling the startActivityForResult() in the NavigationActivity and the setResult(),finish() and onActivityResult() methods in the SearchActivity. But could you please tell me where in the NavigationActivity should I call the startActivityForResult()? – Ingrid Cooper Sep 18 '12 at 08:50
  • It is not quite clear how your application works. In your question you wrote that NagivationActivity is the "previous activity". This indicates that SearchActivity is started by NavigationActivity. If not, please describe the workflow of your application. – Matt Handy Sep 18 '12 at 09:03
  • Yes, the SearchActivity is started by the NavigationActivity only. I called the startActivityForResult() method in the NavigationActivity and the setResult(), finish() and onActivityResult() methods in the SearchActivity but its still not working. The onActivityResult() method is not getting called. – Ingrid Cooper Sep 18 '12 at 09:38
  • No, `onActivityResults()` needs to be in the calling method (`NavigationActivity`). – Matt Handy Sep 18 '12 at 09:58
  • Thank you for your reply. I called the onActivityResult() method in the NavigationActivity and it worked. – Ingrid Cooper Sep 18 '12 at 10:30
0

startActivityForResult is used to return data from called Activity to calling activity, i.e. if activity A is calling B, you can use startActivityForResult to return data from B to A

Nermeen
  • 15,883
  • 5
  • 59
  • 72