Reputation: 955
I am using startActivityForResult() [in MainActivity class] to call another activity called "EditActivity" & get some edited values back to the MainActivity. EditActivity has a button which when pressed should return the user to the MainActivity. But instead the app is closing on button-press. Inside MainActivity :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, EditActivity.class);
intent.putExtra("edit_data", arrayList.get(position).toString());
intent.putExtra("edit_position", position);
startActivityForResult(intent, IntentValuesClass.REQUEST_CODE);
}
});
Inside EditActivity:
saveButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data=new Intent();
data.putExtra("edited_text",editInput);
data.putExtra("position",pos);
setResult(EditActivity.RESULT_OK,data);
Log.w(IntentValuesClass.Msg,"Button SAVE Clicked");
finish();
Log.w(IntentValuesClass.Msg,"finished");
}
}
);
Again on returning to MainActivity:
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data){
Log.w(IntentValuesClass.Msg,"Inside onActivityResult");
int position;
if (requestCode == IntentValuesClass.REQUEST_CODE) {
if (resultCode==EditActivity.RESULT_OK) {
Log.w(IntentValuesClass.Msg,"Result ok");
String s = data.getStringExtra("edited_text");
position = data.getIntExtra("position",-1);
arrayList.remove(position);
arrayList.add(position, s);
adapter.notifyDataSetChanged();
}
}
}
Upvotes: 0
Views: 47
Reputation: 5329
put the finish()
at the end of your method.
public void onClick(View v) {
Intent data=new Intent();
data.putExtra("edited_text",editInput);
data.putExtra("position",pos);
setResult(EditActivity.RESULT_OK,data);
Log.w(IntentValuesClass.Msg,"Button SAVE Clicked");
Log.w(IntentValuesClass.Msg,"finished");
finish();
}
Upvotes: 1