• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help with SaveInstanceState

Crathes

Lurker
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:
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));
        }
    }
}
 
First, welcome to AF!

A couple of quick pointers:

Activity | Android Developers

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.
The bundle in the saved instance state is "smart" to some extent but you'll need to manage some of it yourself if it doesnt appear to be setting certain values as you would want them. But it looks like you understand that.

What you should also look at is how configuration changes work:
Activity | Android Developers


Anyways, it sounds to me like you understand the concepts perfectly well enough, but are having trouble with text displaying in the wrong places, which makes me wonder if there is some other code at work here. Because (at a glance) I don't see any issue with what you have posted.

I would also suggest you take the time to choose more descriptive variable names instead of tAll tallTeTall and such. It will make them much easier to read, and thus debug -- and 90% of coding is debugging! :)

Better names might be like:

customerNameTextView

or

customerNameTV

hope that helps :)
 
Thanks for the answer.

I'll just have to check through everything until i find something then.

The names actually make (kind of) sense in my language, but i agree that they could be better :P
 
Update on the project:

I put all the code that gets the information out of the textviews and texteditors in onPause() like this:
Code:
protected void onPause(Bundle savedInstanceState) {    	
    	// save screen state
		savedInstanceState.putString(textTeller, textTall.getText().toString());
    	savedInstanceState.putString(textListe, textAlleTall.getText().toString());
    	savedInstanceState.putString(txt1Tekst, txt1.getText().toString());
    	super.onSaveInstanceState(savedInstanceState);
    }

I also put the part that fetches the saved information in onCreate() like this:
Code:
if (savedInstanceState != null) {
    		textTall.setText((String)savedInstanceState.getString(textTeller));
    		textAlleTall.setText((String)savedInstanceState.getString(textListe));
    		txt1.setText((String)savedInstanceState.getString(txt1Tekst));
    }

The problem i have now is that the information from the two text views (now saved in Strings renamed to textListe and textTeller) doesnt get recalled. The text in the texteditor gets recalled, but nothing else. Does anone have an ideas?
 
Back
Top Bottom