1

I'm developing a quiz app. The situation is:

  • The user chooses a quiz by touching a button, that opens a new Activity. Inside of this activity the user has to write something on a TextView and than press another button to see if the text is right. If it is right, then another last activity is opened.

  • At the moment, when the user returns back to the main activity, where he has to choose the quiz, if he chooses the same quiz he has to write down the answer again.

I need to find a way to save the answer, so that the app doesn't display the activity where he has to write down the answer anymore. It should jump immediately to the last activity. I know I can manage this using SharedPreferences, but i don't know ho to set it up.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Giulio Tedesco
  • 179
  • 1
  • 3
  • 14
  • 1
    Have you even given it a try? [The docs](http://developer.android.com/guide/topics/data/data-storage.html#pref) have a good example that should at least get you started then you can post your code here explaining the problem – codeMagic Jan 21 '14 at 21:25
  • 6
    This question appears to be off-topic because there doesn't appear to be any attempt at solving the problem. – codeMagic Jan 21 '14 at 21:25
  • Is it just a single answer that has to be remembered? – Michael Yaworski Jan 21 '14 at 21:33
  • I'm sorry it was late night :( i gave a try with shared preferences and internal storage, but it didn't work :( – Giulio Tedesco Jan 22 '14 at 13:38

1 Answers1

0

First, add these methods - used to write to and read from the SharedPreference:

public String getAnswer()
{
   SharedPreferences sp = getSharedPreferences("prefName",0);
   return sp.getString("myAnswer1","TheDefaultValueIfNoAnswerHasBeenSet");
}

public void setAnswer(String theAnswer)
{
   SharedPreferences.Editor editor = getSharedPreferences("prefName",0).edit();
   editor.putString("myAnswer1", theAnswer);
   editor.commit();
}

Then, when the user returns back to the main activity (you can use the onBackPressed method to check when the activity is closing), you can write the answer to the preference.

For example, in your quiz activity:

@Override
public void onBackPressed() {
    setAnswer(/* the String answer the user currently has */);
}

And then when you are opening your quiz activity, you can read from the SharedPreference.

For example, in your onCreate method:

String previousAnswer = getAnswer();

// check to see if the answer is not "TheDefaultValueIfNoAnswerHasBeenSet"
// or whatever you set the default value to be

// set the answer in the quiz to be previousAnswer
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • Thank you so much.. You have answered my stupid (and off-topic) question :( i'm sorry :( – Giulio Tedesco Jan 22 '14 at 14:30
  • Anyway, what should i write in my onCreate method after "String previousAnswer = getAnswer();" the if else statement? Thank you – Giulio Tedesco Jan 22 '14 at 14:31
  • @user3208166 Your question isn't stupid. It's a good one. And you should set the text of the EditText to be the previous answer. Tell me if you still don't understand. I'm not sure how new you are to Android. – Michael Yaworski Jan 22 '14 at 19:54
  • Yes i'm new :) i solved the problem thanks to this thread: http://stackoverflow.com/questions/9964480/how-to-display-login-screen-only-one-time Thanks! Now i'm working on a Reset Button, to reset every single data if the user needs to restart the game :P – Giulio Tedesco Jan 23 '14 at 13:09