Hello
I am programming a small test app for my android phone.
It has a text area and some buttons and labels.
The test is supposed to teach me how i can save states, so that when i turn the phone the text is still there.
My problem is that the text i write into the text area is placed in the two labels as well, were the information i have put into them should be.
So that you have some information to go with. The main part of the program looks like this:
The instance saving and restoring part looks like this:
I am programming a small test app for my android phone.
It has a text area and some buttons and labels.
The test is supposed to teach me how i can save states, so that when i turn the phone the text is still there.
My problem is that the text i write into the text area is placed in the two labels as well, were the information i have put into them should be.
So that you have some information to go with. The main part of the program looks like this:
Code:
public class Main extends Activity implements View.OnClickListener{
Button btnok, btnmin, btnplus;
TextView textTall, textAlleTall;
EditText txt1;
int tall = 0;
String textTallTall = "";
String textAlleTallTall = "";
String txt1Tekst = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnok = (Button)findViewById(R.id.btnok);
btnmin = (Button)findViewById(R.id.btnmin);
btnplus = (Button)findViewById(R.id.btnplus);
textTall = (TextView)findViewById(R.id.textTall);
textAlleTall = (TextView)findViewById(R.id.textAlleTall);
txt1 = (EditText)findViewById(R.id.txt1);
textTall.setText(String.valueOf(tall));
textAlleTall.setText("");
//btn = new Button(this);
btnok.setOnClickListener(this);
btnmin.setOnClickListener(this);
btnplus.setOnClickListener(this);
}
public void onClick(View view) {
switch(view.getId()){
case R.id.btnok:
textAlleTall.append("\n Tallet var:" + tall);
tall=0;
textTall.setText(String.valueOf(tall));
break;
case R.id.btnmin:
tall--;
textTall.setText(String.valueOf(tall));
break;
case R.id.btnplus:
tall++;
textTall.setText(String.valueOf(tall));
break;
}
}
The instance saving and restoring part looks like this:
Code:
@Override
protected void onSaveInstanceState(Bundle outState) {
// save screen state
outState.putString(textTallTall, textTall.getText().toString());
outState.putString(textAlleTallTall, textAlleTall.getText().toString());
outState.putString(txt1Tekst, txt1.getText().toString());
super.onSaveInstanceState(outState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// restore screen state
if (savedInstanceState != null) {
textTall.setText((String)savedInstanceState.getString(textTallTall));
textAlleTall.setText((String)savedInstanceState.getString(textAlleTallTall));
txt1.setText((String)savedInstanceState.getString(txt1Tekst));
}
}
}

