Android app in language environment switch

Source: Internet
Author: User

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&GT;&NBSP;CLS)  {         StartActivity (New intent (THIS,&NBSP;CLS));     }}


Android app in language environment switch

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.