Reputation: 1211
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.
Upvotes: 1
Views: 9058
Reputation: 408
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();
}
});
}
}
Upvotes: 1
Reputation: 30025
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.
Upvotes: 1
Reputation: 15973
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
Upvotes: 0