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();
}
}
d
is yourIntent
variable inonActivityResult()
so try changingto
If this doesn't fix your problem then please be a little more clear on what is/isn't happening.
Also, I agree with Monad's comment. While it shouldn't be causing a problem it does seem a little strange to do all of this on
onCreate()
and you may be a little better off with aButton
or some user input. Otherwise, there really is no need for a separateActivity
but you could use a function to do the calculation in the firstActivity
.Edit
From the Docs
so this won't return the
Intent
passed from the secondActivity
insetResult()
. It will return anIntent
that started thisActivity
that you would use inonCreate()
possibly. In this situation, theActivity
isn't being started but brought back to the top of the stack when the secondActivity
is finished andsetResult()
is called.