sharedpreferences - State not saved after change language in android -


i have implement app in have change language after click on list item , changed after kill app state of language not saved..... though have save sharedpreference....

here code:

public class languages extends fragment { // private int current = -1; view view; context ct; sharedpreferences sp1, sp; boolean ischeck = false; string user_id, countryid; button settings; // string[] arrlanguages = new string[]{"english", "arabic"}; string[] arrlanguages = new string[] { "english", "arabic" }; string req, locale; listview lv; private locale mylocale;  public languages(context c) {     // todo auto-generated constructor stub     this.ct = c; }  /** called when activity first created. */ @override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {     // todo auto-generated method stub      view = inflater.inflate(r.layout.languagechange, container, false);     sharedpreferences sp = getactivity().getsharedpreferences("key", 0);     user_id = sp.getstring("userid=", user_id);     countryid = sp.getstring("countryid", countryid);     settings = (button) view.findviewbyid(r.id.btnsett);     lv = (listview) view.findviewbyid(android.r.id.list);     lv.setadapter(new mobilearrayadapter(ct, arrlanguages));     settings.setonclicklistener(new onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub             fragmentmanager manager = getfragmentmanager();             manager.popbackstack();             log.d("back home", "back button pressed");         }     });     loadlocale();     lv.setonitemclicklistener(new onitemclicklistener() {          @override         public void onitemclick(adapterview<?> parent, view view,                 int position, long id) {              // selected items             string selectedvalue = (string) lv.getadapter().getitem(                     position);             toast.maketext(ct, selectedvalue, toast.length_short).show();             // setresult(result_ok, new intent().putextra("language",             // arrlanguages[position]));             // configuration config = new configuration();             string lang = "en";             switch (position) {             case 0:                 lang = "en";                 break;             case 1:                 lang = "ar";                 break;              default:                  break;             }              changelang(lang);             // getresources().updateconfiguration(config, null);              lv.setitemchecked(position, true);          }      });     return view; }  public void loadlocale() {     string langpref = "language";     sharedpreferences prefs = ct.getsharedpreferences("commonprefs",             activity.mode_private);     string language = prefs.getstring(langpref, "");     changelang(language); }  public void savelocale(string lang) {     string langpref = "language";     sharedpreferences prefs = ct.getsharedpreferences("commonprefs",             activity.mode_private);     sharedpreferences.editor editor = prefs.edit();     editor.putstring(langpref, lang);     editor.commit(); }  public void changelang(string lang) {     if (lang.equalsignorecase(""))         return;     mylocale = new locale(lang);     savelocale(lang);     locale.setdefault(mylocale);     android.content.res.configuration config = new android.content.res.configuration();     config.locale = mylocale;     ct.getresources().updateconfiguration(config,             ct.getresources().getdisplaymetrics());  }  class mobilearrayadapter extends arrayadapter<string> {     private final context context;     private final string[] values;      public mobilearrayadapter(context context, string[] values) {         super(context, r.layout.list_changelanguage, values);         this.context = context;         this.values = values;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         layoutinflater inflater = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);          view rowview = inflater.inflate(r.layout.list_changelanguage,                 parent, false);         textview textview = (textview) rowview.findviewbyid(r.id.textview1);         imageview imageview = (imageview) rowview                 .findviewbyid(r.id.imageview1);         textview.settext(values[position]);          // change icon based on name         string s = values[position];          system.out.println(s);         if (lv.isitemchecked(position)) {             imageview.setvisibility(view.visible);             ischeck = true;             sharedpreferences sp = getactivity().getsharedpreferences(                     "check", 0);             sharedpreferences.editor sedt = sp.edit();             sedt.putboolean("ischeck", ischeck = true);              sedt.clear();              sedt.commit();         } else {             imageview.setvisibility(view.gone);          }          return rowview;     } }  @override public void onconfigurationchanged(         android.content.res.configuration newconfig) {     super.onconfigurationchanged(newconfig);     if (mylocale != null) {         newconfig.locale = mylocale;         locale.setdefault(mylocale);         ct.getresources().updateconfiguration(newconfig,                 ct.getresources().getdisplaymetrics());     } } 

try change.

lv.setonitemclicklistener(new onitemclicklistener() {

        @override         public void onitemclick(adapterview<?> parent, view view,                 int position, long id) {              // selected items             string selectedvalue = (string) lv.getadapter().getitem(                     position);             toast.maketext(ct, selectedvalue, toast.length_short).show();             // setresult(result_ok, new intent().putextra("language",             // arrlanguages[position]));             // configuration config = new configuration();             string lang = "" + selectedvalue;              changelang(lang);             // getresources().updateconfiguration(config, null);              lv.setitemchecked(position, true);          }      });     return view; } 

Comments