-4

I'm building an application that asks the user to select one option (41 languages) from multiple choices on application launch and in bottom there is a toggle button wich is off by default. If the user sets it on "on" I want my application to remember this selection and go to that language option at launch, ignoring the language selection page.

I'm pretty new to android development, I would appreciate if the answer would include some example code so I can modify it and learn while modifying it.

Thank you.

C.S.
  • 279
  • 1
  • 6
  • 21

1 Answers1

2

SharedPreferences is what you need.

// here you get an instance of SharedPreferences for your app
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

// editor is required to apply changes to existing SharedPreferences
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.apply();

// this is a way that you take stored values back from the SharedPreferences
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66