I am a beginner and I am trying to make an Intent Calculator, means I am add the inputs through Intent extras and starting a new activity for calculating the input. Finally I am trying to receive the result using onActivityResult(). The problem is i have set the result to text view, the text view detects it as a double value(displaying 0.0 from default text). I cant understand where the problem is as there are no errors in Logcat even. Below is my code. When I press the button it just displays 0.0. No matter how many times i click. Also I am practicing Intents so just tried the basic one. Kindly help me.
public class InputActivity extends Activity {
final int CALCULATION_COMPLETE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
final EditText num1 = (EditText) findViewById(R.id.num1_edit);
final EditText num2 = (EditText) findViewById(R.id.num2_edit);
Button add = (Button) findViewById(R.id.resultButton);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String firstNum = num1.getText().toString();
String secondNum = num2.getText().toString();
Intent resultIntent = new Intent(InputActivity.this,ResultActivity.class);
resultIntent.putExtra("first", firstNum);
resultIntent.putExtra("second", secondNum);
startActivityForResult(resultIntent, CALCULATION_COMPLETE);
}
});
}
@Override
protected void onActivityResult(int reqCode, int resCode, Intent d){
super.onActivityResult(reqCode, resCode, d);
TextView result = (TextView) findViewById(R.id.result);
if(reqCode == CALCULATION_COMPLETE && resCode == RESULT_OK){
Intent resultData = getIntent();
Double addResult = resultData.getDoubleExtra("result", 0);
String resultStr = addResult.toString();
result.setText(resultStr);
}
}
}
public class ResultActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent calcIntent = getIntent();
String firstStr = calcIntent.getStringExtra("first");
String secondStr = calcIntent.getStringExtra("second");
Double firstNum = Double.parseDouble(firstStr);
Double secondNum = Double.parseDouble(secondStr);
Double addResult = firstNum + secondNum;
Intent result = new Intent();
result.putExtra("result", addResult);
setResult(RESULT_OK, result );
finish();
}
}