1

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));
}

Drawing of Activities

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

Robert
  • 1,107
  • 2
  • 8
  • 8
  • I've read your question several times, and I still don't understand *what* you are trying to achieve. Forget the code and the life cycle for now, what is your expected result? – Simon May 01 '13 at 21:09
  • 1
    Hi Simon iv added a picture to try and explain it a bit better. – Robert May 03 '13 at 14:16

3 Answers3

2

Save your values in a bundle, I can give you the code to do so in a second of you would like.

Drew Stauft
  • 107
  • 11
1

Write values example

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInsta nceState);

    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
    savedInstanceState.putBoolean("MyBoolean", true);
    savedInstanceState.putDouble("myDouble", 1.9);
    savedInstanceState.putInt("MyInt", 1);
    savedInstanceState.putString("MyString", "Welcome back to Android");
    // etc.
}

Extract values example

@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedIn stanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
    double myDouble = savedInstanceState.getDouble("myDouble");
    int myInt = savedInstanceState.getInt("MyInt");
    String myString = savedInstanceState.getString("MyString");
}
Edward Falk
  • 9,991
  • 11
  • 77
  • 112
Drew Stauft
  • 107
  • 11
  • Hi iv done this now, but now its not adding number when activity is resumed it only adds them if i rotate the phone. so still the same problem. – Robert May 02 '13 at 18:13
-1

You can detect device rotation by this answer. Disable your function in onResume() and onPause() when you detect rotation. Hope this helps.

Community
  • 1
  • 1
ACengiz
  • 1,285
  • 17
  • 23
  • Handling rotation or other configuration changes is discouraged because it's very hard to do right. Plus, it won't solve OP's problem because there are a hundred other reasons why the app might be taken away and restarted. See my notes at http://stackoverflow.com/questions/3250987/save-cache-when-rotate-device/3252547#3252547 – Edward Falk May 01 '13 at 21:51