1

For a personal project I am making a calculator that can parse a regular expression from a user inputted string.

My main problem right now is that if the user inputs some input, say 1 + 2 , then rotates the device, although I have managed to get my program to preserve the 1 + 2 after rotation, any additional inputs from the user will wipe it out and replace it entirely with the next input. So if I inputted 1 + 2, then rotated the device, then inputted + 4, the device would read + 4 instead of 1 + 2 + 4 like I was expecting.

Here is my code so far, thank you for your help in advance:

public class MyCalculator extends Activity {

String displayStr = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_calculator);
    Button btnClear = (Button) findViewById(R.id.btnClear);

    btnClear.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            displayStr = "";
            TextView et= (TextView) findViewById(R.id.display);
            et.setText(displayStr);
        }
    });
}

public void enterDigit(View view){
    TextView et = (TextView) findViewById(R.id.display);
    Button button = (Button)view;
    if(et.getText().toString().compareTo("Unable to Parse") == 0){
        displayStr = (String) button.getText();
    }
    else{
        displayStr += button.getText();
    }
    et.setText(displayStr);

}

public void enterOperation(View view){
    TextView et = (TextView) findViewById(R.id.display);
    Button button = (Button)view;
    if(et.getText().toString().compareTo("Unable to Parse") == 0){
        displayStr = (String) button.getText();
    }
    else{
        displayStr += " " + button.getText() + " ";
    }
    et.setText(displayStr);

}

public void enterClick(View v){ //TBD error checking
    String s = "";
    TextView et  = (TextView) findViewById(R.id.display);
    s = et.getText().toString();
    s = TestParser.parse(s);
    et.setText(s);
    displayStr= "";

}
}

1 Answers1

1
  1. If you are setting the values you could implement:

    @Override
    public void onSaveInstanceState(Bundle out)
    {
        TextView et = (TextView) findViewById(R.id.display);
        out.putString("etText", et.getText().toString());
        //OR if it is always displayString
        out.putString("displayString", displayString);
    }
    
    @Override
    public void onRestoreInstanceState(Bundle in)
    {
        String prevText = (String)in.getString("etText");
        displayString = (String)in.getString("displayString");
        TextView et = (TextView) findViewById(R.id.display);
        et.setText(prevText);
        et.invalidate();
    }
    

which just saves and stores values when the activity's state changes OR

  1. implement onConfigurationChanged as per this example:

How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE in android 2.3.3

Community
  • 1
  • 1
KoalaKoalified
  • 687
  • 4
  • 15
  • So unfortunately, what I am doing is basically appending to a string, and then taking that completed string and parsing it for the mathematical expression. Interestingly enough, my calculator parses the rotated text just fine, it just deletes whatever was there before if I try to add anything to it. – user5709229 Dec 23 '15 at 01:18
  • My guess is that if you rotate it twice in a short span of time, it will lose all reference to the value right? – KoalaKoalified Dec 23 '15 at 01:22
  • well in either case what should work is to store a reference to that string in onSaveInstanceState and retrieve that reference in onRestoreInstanceState EDIT: Well if you don't have autocomplete it is something like: (SaveInstance) out.putString("keyToRetrieveInRestore", StringValue); (Restore) StringValue = (String)in.getString("keyToRetrieveInRestore"); – KoalaKoalified Dec 23 '15 at 01:25
  • Rotating it any number of times in a short span seems to be fine. It's just any kind of modification to the string that wipes the string out. Such a weird little bug. – user5709229 Dec 23 '15 at 01:32
  • I figured something out. Thanks for your help. – user5709229 Dec 23 '15 at 01:41