Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Activity Lifecycle The Activity Lifecycle Saving and Retrieving Instance State

Android activity lifecycle TextView. I cannot see which text or string needs to be restored.

KEY_USERKEY must have both String and int values. I can't see the String value Steve Hunter

MainActivity.java
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {

  public static final String KEY_USERENTRY = "KEY_USERKEY";    
  public TextView mTextView;
  private int x = 10;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = (TextView) findViewById(R.id.textView);
  }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
      outState.putInt(KEY_USERENTRY, 10);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
      mTextView = savedInstanceState.getString();


    }
}

1 Answer

Hi Mal,

In onRestoreInstanceState, call getInt() on the parameter being received, savedInstanceState, and pass in the constant that you defined in the first part of the challenge. This key is associated with the value from onSaveInstanceState that you set earlier - 10. Note, my constant is called something different to yours, so change that to fit.

This will return an int, 10. We want to use this to be the value held by mTextView which we will set using setText(). That is expecting a String as a parameter, not an int. Use a moosh to perform the type cast. All that looks like:

        mTextView.setText(savedInstanceState.getInt(KEY_USERENTRY) + "");

I hope that helps,

Steve.