I'm currently on my first steps with android. I'm developing a very simple calculator in which I have two activities, one main activity and a result activity. On this result activity I have a return button which takes to he main activity. the problem is that when I return to the main activity the values I ut on the text fields are still there. How can I do so that when I return from the result activity the fields on the main activity are cleared?
Here's the code for my main activity:
public class MainActivity extends Activity implements OnCheckedChangeListener{
private static final int REQUEST_CODE = 1;
private EditText number1, number2;
private Button btn1;
private Intent intent;
private String operation;
private RadioGroup RadioGroupOperation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//faz a ligação entre a aplicação e o layout
number1 = (EditText)findViewById(R.id.editText1);
number2 = (EditText)findViewById(R.id.editText2);
btn1 = (Button)findViewById(R.id.calcular);
intent = new Intent( this, ResultActivity.class);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//insere na intent os dois numeros digitados pelo utilizador
intent.putExtra("number1", number1.getText().toString());
intent.putExtra("number2", number2.getText().toString());
intent.putExtra("operation", operation);
startActivityForResult(intent, REQUEST_CODE);
}
});
RadioGroupOperation = (RadioGroup)findViewById(R.id.radioGroup1);
RadioGroupOperation.setOnCheckedChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.soma:
operation="+";
break;
case R.id.subtracao:
operation="-";
break;
case R.id.multiplicacao:
operation="*";
break;
case R.id.divisao:
operation="/";
break;
}
Toast.makeText(MainActivity.this, operation, Toast.LENGTH_SHORT).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if( resultCode == RESULT_OK && requestCode == REQUEST_CODE ){
if(data.hasExtra("result")){
Toast.makeText(this, "O resultado da operação anterior foi:" + data.getExtras().getString("result") , Toast.LENGTH_LONG).show();
}
//testing
/*if(data.hasExtra("number1") && data.hasExtra("number2")){
number1.setText(0);
number2.setText(0);
}*/
//end testing
}
}
}
And my second activity (result):
public class ResultActivity extends Activity {
private TextView textView1;
private Intent intent;
private String number1, number2, operation;
private double result;
private Button btn1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
btn1 = (Button)findViewById(R.id.voltar);
intent = getIntent();
//armazena os numeros digitados
number1 = intent.getStringExtra("number1");
number2 = intent.getStringExtra("number2");
operation = intent.getStringExtra("operation");
//Efectua calculos
try {
if(operation.equals("+"))
result = (Double.parseDouble(number1) + Double.parseDouble(number2));
else if(operation.equals("-"))
result = (Double.parseDouble(number1) - Double.parseDouble(number2));
else if(operation.equals("*"))
result = (Double.parseDouble(number1) * Double.parseDouble(number2));
else if(operation.equals("/"))
result = (Double.parseDouble(number1) / Double.parseDouble(number2));
textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText("O resultado é " + String.valueOf(result));
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
catch(Exception e){
Toast.makeText(this, "Ocorreu um erro!", Toast.LENGTH_SHORT).show();
this.finish();
}
}
@Override
public void finish(){
Intent data = new Intent();
data.putExtra("result", String.valueOf(result));
//testing
//data.putExtra("number1", 0);
//data.putExtra("number2", 0);
//end testing
setResult(RESULT_OK, data);
super.finish();
}
}
I already tried to do that, if you see the blocks between //testing and //end testing but the result was that I crashed the app when I tried to go back to the first activity.
Any idea on how I can do this?
Thanks in advance, CT
Everything is right except setting text to 0. EditText.setText accepts only string value.
Change your test code to
if(data.hasExtra("number1") && data.hasExtra("number2")){
number1.setText("0");
number2.setText("0");
}
Only change needed is setText("0")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With