I have some issues while calling startActivityForResult from onClickListener.
In the first activity i have 2 ways of starting startActivityForResult
-
ListViewwithOnItemClick()-when i’m callingstartActivityForResultfrom theListView OnItemClick()it works just fine -
ButtonwithOnClickListener()-but when i’m calling startActivityForResult from theButton OnClickListener()the second activity wont startOnActivityResultfrom some reasone.
here is my code:
*Update:
thank you for your time, what you offerd is one of the changes I allready tried, however its stays the same my new code:
first call with StartActivityForResult() ( Works Fine! – inside OnCreate() ):
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
Intent intent = new Intent(NoteNames.this, EditNote.class);
intent.putExtra("subject", noteList.get(position).getSubjectText());
intent.putExtra("body", noteList.get(position).getBodyText());
startActivityForResult(intent, position);
}
});
Second call: wont start OnActivityResult():
@Override
public void onClick(View v) {
if(v.getId() == addButton.getId()){
Intent intent = new Intent(NoteNames.this, EditNote.class);
intent.putExtra("subject", "");
intent.putExtra("body", "");
startActivityForResult(intent, idOfNew);
}
}
this is the SecondActivity:
private EditText subjectEditText, bodyEditText;
private Button approve, decline;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_edit_note);
subjectEditText = (EditText) findViewById(R.id.note_head);
bodyEditText = (EditText) findViewById(R.id.note_text);
approve = (Button) findViewById(R.id.approve_button);
decline = (Button) findViewById(R.id.decline_button);
Intent intent = getIntent();
String subject = intent.getExtras().getString("subject");
String body = intent.getExtras().getString("body");
subjectEditText.setText(subject);
bodyEditText.setText(body);
Handler handler = new Handler();
approve.setOnClickListener(handler);
decline.setOnClickListener(handler);
super.onCreate(savedInstanceState);
}
class Handler implements OnClickListener{
@Override
public void onClick(View v) {
if (v.getId()==approve.getId()){
Intent intent = getIntent();
intent.putExtra("subject", String.valueOf(subjectEditText.getText()));
intent.putExtra("body", String.valueOf(bodyEditText.getText()));
setResult(RESULT_OK, intent);
finish();
}
}
}
}
EDIT: Before you edited your question, in your code was written
final int idOfNew=-5. Well, if therequestCode<0then theonActivityResultis not triggered. Use a request code>0(maybe the id of the button), this should solve your problem. Everything else in your code is just fine.