The logic is simple:
1 All activity within the app inherits from Baseactivity or baseactivity derived subclasses, and Baseactivity maintains a static app activity access stack that performs both stack and stack operations when created and destroyed. So Mlocalstack is the activity that is running in the app.
2 Store the app's locale in sharedpreferences, avoid changes to the state when the app restarts, remove the locale string when baseactivity is created, and initialize the activity locale (Initlang method).
3 in the page switch language, call Baseactivity's Switchlang method, finish all currently running activity, and jump to the app main page.
public abstract class baseactivity extends activity { // activity Access Stack private static stack<baseactivity> mlocalstack = new Stack<BaseActivity> (); protected baseactivity mcontext; @ Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); mContext = this; // Press Stack mlocalstack.add (This); initlang (); } @Override protected void ondestroy () &nbsP { // Mlocalstack.remove (this); super.ondestroy (); } private void initlang () { resources res = getresources (); Configuration config = res.getconfiguration (); Locale locale = locale.getdefault (); string langStr = ""; //todo Gets the app locale stored in sharedpreferences if ("zh". Equals (LANGSTR)) { locale = Locale.CHINA; }else if ("en". Equals (Langstr)) { locale = Locale.ENGLISH; }else if{ //..... } config.locale = locale; res.updateconfiguration (Config, res.getdisplaymetrics ()); } protected void switchlang (String newLang) { //TODO update the app locales stored in sharedpreferences // finish All activity while in App memory (0 != mlocalstack.size () ) { &nbsP; mlocalstack.pop (). Finish (); } / / Jump to App home startactivity (Mainactivity.class); } public void startactivity (Class<? &NBSP;EXTENDS&NBSP;BASEACTIVITY>&NBSP;CLS) { StartActivity (New intent (THIS,&NBSP;CLS)); }}
Android app in language environment switch