0

when my app get crashed for any reason or force close i hit OK button on the message says "unfortunately the app has been stopped" . then my app is come back again to same activity , but it get it in other Language set and call OnResume()

my Question is how to make my app return to same Language config even when my app has been crashed for some reason.

I'm trying to refresh and get the last config was set in sharedPreferences and refresh my config or content view but no luck

Code :

 @Override
public void onResume(){
    super.onResume();

    if(appSharedPrefs.getBoolean("IsArabic",true)){
        Log.d("ERRRRR", "AR"); // that is called 
        setLocale(getApplicationContext(), "ar");
    }
    else{
        Log.d("ERRRRR", "EN");
        setLocale(getApplicationContext(), "en");
    }
}

protected  void setLocale(final Context ctx, final String lang)
{
    Log.d("ERRRRR", lang);

  /*  final Locale loc = new Locale(lang);
    Locale.setDefault(loc);
    final Configuration cfg = new Configuration();
    cfg.setLocale(loc);
    ctx.getResources().updateConfiguration(cfg, null);*/




    Resources res = ctx.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(lang.toLowerCase());
    res.updateConfiguration(conf, dm);
}


@Override
public void onConfigurationChanged(Configuration newConfig) {
    Log.d("ERRRRR", "onConfigurationChanged");
    super.onConfigurationChanged(newConfig);
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62

1 Answers1

1

try this:

you can catch the crash event and load your activity with your desired locale and configuration...

use this utility class:

 public class ExceptionHandler implements
        Thread.UncaughtExceptionHandler {
    private final Activity myContext;


    public ExceptionHandler(Activity context) {
        myContext = context;
    }

    public void uncaughtException(Thread thread, Throwable exception) {

        //you can show a dialog here telling that something went wrong
        Intent intent = new Intent(myContext, your_activity.class); //start your activity
        //put your locale you can use shared preference also
        intent.putExtra("locale", "ar");
        myContext.startActivity(intent);

        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(10);
    }
}

Now use it in your Mainactivity onCreate like

Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • my problem is not in SharedPreferences , it works for me , but after getting the right Lang , the layout still working in other Lang after my app is crashed – Hashoma El-Nemr Jan 17 '17 at 17:03
  • after the app got crashed ...shared preferences loses it's value so your `appSharedPrefs.getBoolean` gives you wrong value – rafsanahmad007 Jan 17 '17 at 17:04
  • see this : http://stackoverflow.com/questions/15353900/shared-preferences-reset-data-when-app-is-force-closed-or-device-is-restarted – rafsanahmad007 Jan 17 '17 at 17:05
  • i have tested it without getting from SharedPreferences , make it arabic Lang in both values , but still get wrong layout LTR – Hashoma El-Nemr Jan 17 '17 at 17:07
  • i used this code : Resources res = myContext.getResources(); // Change locale settings in the app. DisplayMetrics dm = res.getDisplayMetrics(); android.content.res.Configuration conf = res.getConfiguration(); conf.locale = new Locale("ar"); res.updateConfiguration(conf, dm); – Hashoma El-Nemr Jan 17 '17 at 17:08
  • but no effect is was taken after this code runs out – Hashoma El-Nemr Jan 17 '17 at 17:08
  • after your app got crashed you activity is restarted and onResume called..it then loads language form `sharedpreferences`? – rafsanahmad007 Jan 17 '17 at 17:18
  • Yes , i have tested from sharedpreferences and without sharedpreferences , I used it to force to load arabic but not effect is taken , my layout still in English state from left to right , it should be on arabic from right to left as i want – Hashoma El-Nemr Jan 17 '17 at 17:25
  • it's load the english xml layout insted of arabic xml layout , how to force it to load arabic xml layout only when activity restart ? – Hashoma El-Nemr Jan 17 '17 at 17:36
  • you mean when the app got crashed? – rafsanahmad007 Jan 17 '17 at 17:37
  • thank you , but i was tested your same code , but it also getting the wrong layout English left to right – Hashoma El-Nemr Jan 17 '17 at 18:21