Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Activity Lifecycle The Activity Lifecycle Retrieving Instance State

Jarvis Chandler
Jarvis Chandler
7,619 Points

App seems to rest the color back to green and give me the Toast message "Yay! Our Activity was created!"

nfacts;

import android.content.Intent; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast;

import java.util.Random;

public class FunFactsActivity extends AppCompatActivity {

public static final String TAG = FunFactsActivity.class.getSimpleName();
private static final String KEY_FACT = "KEY_FACT";
private static final String KEY_COLOR = "KEY_COLOR";
private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel = new ColorWheel();
private String mFact = mFactBook.mFacts[0];
private int mColor = Color.parseColor(mColorWheel.mColors[8]);
private RelativeLayout mRelativeLayout;
private Button mShowFactButton;
private TextView mFactLabel;

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putString(KEY_FACT, mFact);
    outState.putInt(KEY_COLOR, mColor);
}

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

    mFact = savedInstanceState.getString(KEY_FACT);
    mRelativeLayout.setBackgroundColor(mColor);
    mFactLabel.setText(mFact);
    mShowFactButton.setTextColor(mColor);
    mColor = savedInstanceState.getInt(KEY_COLOR);
}

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

    mFactLabel = (TextView) findViewById(R.id.factTextView);
     mShowFactButton = (Button) findViewById(R.id.showFactButton);
     mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mFact = mFactBook.getFacts();
            mFactLabel.setText(mFact);

            mColor = mColorWheel.getColor();
            mRelativeLayout.setBackgroundColor(mColor);
            mShowFactButton.setTextColor(mColor);

        }
    };
    mShowFactButton.setOnClickListener(listener);

Toast.makeText(this, "Yay! Our Activity was created!", Toast.LENGTH_LONG).show();

    Log.d(TAG, "We're logging from the onCreate() method!" );

}

}

Royce Foo Wai Kit
Royce Foo Wai Kit
2,551 Points

Just curious

mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout<<<);

is it supposed to be small letter for relativeLayout or RelativeLayout ?

Jarvis Chandler
Jarvis Chandler
7,619 Points

Not sure? I changed the get color and reentered it and it worked.? Not Really sure how what to say but thanks for the response back.

1 Answer

You get the toast message because of the line before the last line, the "Toast class method" one. That give u the message when you open the app. Just coment that line or remove it from ypur code