Reputation: 7954
I have an MainActivity
which has two TextView
. User has an option to start another activity and choose data from ListView
to fill those TextView
. My main activity has an OnClickListener
which starts an Activty
from which user can select data and come back to main activity.My OnClickListener
code looks likes this:
private static final int PICK_START = 0;
private static final int PICK_END = 1;
@Override
public void onClick(View v) {
Log.i(MainActivity, "view clicked");
int id = v.getId();
if (id == R.id.searchR) {
//do nothing
} else if (id == R.id.startSearch) {
Intent startIntent = new Intent(this, SList.class);
startActivityForResult(startIntent, PICK_START);
} else if (id == R.id.endSearch) {
Intent startIntent = new Intent(this, SList.class);
startActivityForResult(startIntent, PICK_END);
}
}
When the above onClick method gets called and after that its starts another activity SList.class.In that I have a listview from which user can select the value and upon selecting value the result will be set and activity will finish itself.Code for this is:
sListview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Station selectedS = sArray.get(position);
Intent returnIntent = new Intent();
returnIntent.putExtra("_id", selectedS.getId());
returnIntent.putExtra("name", selectedS.getName());
setResult(RESULT_OK, returnIntent);
finish();
}
});
In above code activity sets the result and finishes itself.Till here everything is working accordingly.But after that when the previous activity is started, the onActivityResult()
method nevers gets called.The code for onActivityResult()
is :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(MainActivity, "" + requestCode);
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Check which request we're responding to
if (requestCode == PICK_START) {
//data.getStringExtra("_id");
Log.i(MainActivity, data.getStringExtra("name"));
} else if (requestCode == PICK_END) {
Log.i(MainActivity, data.getStringExtra("name"));
}
}
}
I dont know why onActivityResult is never triggered .Someone even
wrote on his blog that There is bug in android API. In
startActivityForResult(intent, requestCode);
This function does work
as long as requestCode = 0
. However, if you change the request code to
anything other than zero, the ApiDemos will fail (and onActivityResult()
won't be called).
Upvotes: 0
Views: 880
Reputation: 7954
I found the solution to my problem.I just needed to restart my eclipse and my code started working.
Upvotes: 1