android - How to refresh activity after changing language? -
i search on google & stackoverflow can't solve problem. refer http://www.androhub.com/android-building-multi-language-supported-app/ have 3 activities (activity 1,2,3). activity 1 > activity 2 > activity 3 (milti lang change). in activity 3, have 2 radio button change (en, hi). after change language; go first activity (activity 1), ok. want stay current activity (refresh current activity), after choose radio button (changed language). otherwise, how recall oncreate() when go onbackpressed(). how best way stay current activity after change language.
oncreate
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main3); txt = (mmtextview) findviewbyid(r.id.txt); radiogroup = (radiogroup) findviewbyid(r.id.radiogroup); pref = getsharedpreferences("mypref", activity.mode_private); editor = pref.edit(); radiogroup.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() { @override public void oncheckedchanged(radiogroup radiogroup, @idres int i) { string lang = "en"; switch (i) { case r.id.eng: lang = "en"; intent i1 = new intent(main3activity.this, mainactivity.class); i1.addflags(intent.flag_activity_clear_top | intent.flag_activity_new_task); startactivity(i1); finish(); break; case r.id.hi: lang = "hi"; intent i2 = new intent(main3activity.this, mainactivity.class); i2.addflags(intent.flag_activity_clear_top | intent.flag_activity_new_task); startactivity(i2); finish(); break; default: break; } changelocale(lang); } }); loadlocale(); }
changelocale
private void changelocale(string lang) { if (lang.equalsignorecase("")) return; locale mylocale = new locale(lang); savelocale(lang); locale.setdefault(mylocale); configuration config = new configuration(); config.locale = mylocale; getbasecontext().getresources().updateconfiguration(config, getbasecontext().getresources().getdisplaymetrics()); }
savelocale & loadlocale
private void savelocale(string lang) { editor.putstring("save", lang); editor.commit(); } private void loadlocale() { string lang = pref.getstring("save", ""); changelocale(lang); }
you can close , open same activity like:
startactivity(new intent(activityname.this, activityname.class)); finish();
i hope works.
Comments
Post a Comment