What i'm trying to do is save some numbers in an activity so I can add them to more numbers when the activity starts again.
I tried saving the numbers in on Pause and then adding them to new numbers in on Resume, it kinda worked but when i rotate the phone it adds the saved numbers again, whats a better way to go about this
A bit of the code
if (getIntent().hasExtra("ResultBl100")) {
editbl.setText(bl100 + "");
numberOfBl = Integer.parseInt(editbl.getText().toString());
totalSd = (float) (numberOfBl * 0.0024);
editsd.setText(String.format("%.1f", totalSd));
b = Float.parseFloat(editsd.getText().toString());
totalCt = (int) Math.ceil(b * 7);
ct.setText(Integer.toString(totalCt));
}
//sending back to start of app
addAgain = (Button) findViewById(R.id.add_again);
addAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Amounts.this,
ChooseMaterials.class);
intent.putExtra("AddMore", ADD_MORE);
startActivity(intent);
}
});
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (TextUtils.isEmpty(editBl.getText().toString())) {
numberOfBl = 0;
} else {
numberOfBl = Integer.parseInt(editBl.getText().toString();
}
if (TextUtils.isEmpty(editSd.getText().toString())) {
amountSd = 0;
} else {
amountSd = Float.parseFloat(editSd.getText().toString());
}
SharedPreferences prefs = getSharedPreferences(
"SavedTotals", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("savedBl", numberOfBl);
editor.putFloat("savedSd", amountSd);
editor.commit();
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences preferences = getSharedPreferences("SavedTotals",
Context.MODE_PRIVATE);
int blSaved = preferences.getInt("savedBl", 0);
float sdSaved = preferences.getFloat("savedSd", 0);
int numberBl = blSaved + numberOfBl;
editBl.setText(Integer.toString(numberBl));
float amountSds = sdSaved + amountSd;
editSd.setText(Float.toString(amountSds));
}
I'm still stuck on this could someone please give me a bit more help, iv attached a picture to show what i'm trying to achieve.
User starts off on menu which leads to either activity a b or c, where they input some numbers and end up on final activity, where some totals are displayed, when user presses button on last activity, I want to save the totals and send user back to menu where he can start again, when user ends up back on last activity, There will be a new set of totals i want to add the saved total to the new set of totals and display them
Iv tried on Saved Instance State and on Restore Instance State now and its the same problem I was having when I rotate phone, It adds the saved totals again