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 Introducing SharedPreferences Saving and Retrieving Data with SharedPreferences

shared preferences

Hi Steve Hunter. Am now on the android activity lifecycle. Still much more to learn.

MainActivity.java
import android.view.View;
import android.os.Bundle;

public class MainActivity extends Activity {
  public static final String PREFS_FILE;
  public static final String KEY_CHECKBOX;
  public SharedPreferences mSharedPreferences;
  public SharedPreferences.Editor mEditor;
  public CheckBox mCheckBox;
  public EditText mEditText;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
    mEditText = mSharedPreferences.edit();

  }
}

OK - ask a question and then help me out. What did you expect your code to do; what did it do; what error did you get; why do you think that happened?

I'm not going to just chuck you a solution every time! :wink:

Steve.

Kikiki. Yep. In the video PREFS_FILE and KEY_CHECKBOX both have values but not in the code given in the problem.

public static final String PREFS_FILE = preferences; public static final String KEY_CHECKBOX = key_edittext;

I tried assigning them values but that didn't work. Let me get the errors which I got.

Then initialize both of these fields in the onCreate method.

mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
mEditText = mSharedPreferences.edit()

4 Answers

Try:

private static final String PREFS_FILE = "prefs";
private static final String KEY_CHECKBOX = "key_checkbox";

Actually, no. I've not done this course nor do I have any experience in database actions within Android; I don't know the solution.

I'll wrestle with this one. Must have an answer by tomorrow afternoon.

Is this a college course, or something? If I get a minute, I'll work through the video, if that'll help. Tomorrow's quite busy but I'll see if I can manage 20 minutes. And what time zone are you in? It's 21:30 here now.

Yes. When you assign a string to a variable, or constant, you need to treat it as a string, enclosing it with double quotes. So,

public String myName = "Steve Hunter";
public static final String MY_NAME = "Steve Hunter";

Yeah?

Steve.

Ok. So I can use quotes"" outside brackets as well. So in effect if a CONSTANT is a String I have to use quotes to assign it a value?