1

I am saving user selected language in sharedpreference. when i close the app and restart it i read the value in sharedpreference by getValue method, and try to change the language. But my activity is stuck in loop.

I check this in onCreate

 String language = getValue(getBaseContext()); if (language!= null && !language.isEmpty()) { changelanguage(language); } 

changeLanguage is as:

private void changelanguage(String languageToLoad) { Locale myLocale = new Locale(languageToLoad); Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); Intent refresh = new Intent(this, MainActivity.class); startActivity(refresh); finish(); save(this, languageToLoad); } 

How can i read and load user saved language on startup?

6
  • 1
    where are you saving the language in sharedprefernces, you can share some code with us Commented Oct 1, 2015 at 17:12
  • 1
    You're starting the new activity, but only save the language the newly opening activity should use two lines below (assuming your save() method simply does a SharedPreferences.Editor.commit()) Commented Oct 1, 2015 at 17:15
  • public void save(Context context, String text) { SharedPreferences settings; SharedPreferences.Editor editor; settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); editor = settings.edit(); //2 editor.putString(PREFS_KEY, text); editor.commit(); } public String getValue(Context context) { SharedPreferences settings; String text; settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1 text = settings.getString(PREFS_KEY, null); //2 return text; } Commented Oct 1, 2015 at 18:27
  • I use save() function for saving selected language in sharedpreference. GetValue() method for loading the previously saved value. The methods are given above. Commented Oct 1, 2015 at 18:32
  • My guess is " Intent refresh = new Intent(this, MainActivity.class); startActivity(refresh); finish(); ", this is causing the loop. So how can i set the language when user restarts the app? Commented Oct 1, 2015 at 18:34

2 Answers 2

1

Well i solved it by setting the language in oncreate just before the UI is inflated.

Sign up to request clarification or add additional context in comments.

Comments

0

you are restart your intend in on create method this result in infinite loop .just remove
Intent refresh = new Intent(this, MainActivity.class); startActivity(refresh); finish();
this code .hope this will help you

4 Comments

But i need this when user changes the language from action bar button, otherwise the screen is not refreshed after selecting the language. i am calling the same method from onCreate when activity is started and from onOptionsItemSelected when user is changing it. I just dont know howto check if the method is called from onCreate.
start this activity from onOptionItemSelected method but not from on create otherwise it always stuck in loop.
then how can i load the language on app startup from sharedpreference?
get value from sharedpreference if it does't contain any value then you have to call this method from onOptionItemSelected otherwise if it contain any value set that language in on create method itself

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.