I'm making this school projet and I need to get back the result from the Activity I've called
startActivityForResult(Activity2, 100);
Then in the activity two I've this
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(....);
final Intent BackToActivityOne = new Intent(Activity2.this, ActivityOne.class);
if(requestCode = this.REQUESTCODEACTIVITY && resultCode = RESULT_OK && data != null){
//from the activity2
sum = data.getIntExtra("sum",0);
//to the activity1
BackToActivityOne.putExtra("total",sum);
btn1.setOnClickListener(new view.OnClickListener(){
@Override
public void onClick(View view){
BackToActivityOne.putExtra("code",1000);
setResult(RESULT_OK,BackToActivityOne);
finish();
}
});
btn2.setOnClickListener(new view.OnClickListener(){
@Override
public void onClick(View view){
BackToActivityOne.putExtra("code",1001);
setResult(RESULT_OK,BackToActivityOne);
finish();
}
});
}//if
}//onActivityResult(...)
And here is the problem, when I click on btn1 or btn2 it kills my app.
But I want the app to go back to activity1 where I make the treatment
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(....);
if(resultCode = RESULT_OK && data != null){
final int Code = data.getIntExtra("code",0);
if(code == 1000){
//code to do
}
else if(code == 1001){
//code to do
}
}//if
}//onActivityResult(...)
For sure I'm doing something wrong but I can't find what. Hopefully somebody can help me to find the error.
You are mixing two things.
protected void onActivityResultshould be overriden only in activity one.Activity two shold have two buttons as I figured out. yor code:
should be in
onCreatewhere you obtain your references from view. Not inonActivityResult. Remember to get view by id and initialize reference! I don't think that final is necessary for intent.