2017-01-17 16 views
0

後工作時,我的應用程序崩潰得到以任何理由或強制關閉我打的消息OK button說:「unfortunately the app has been stopped」。然後我的應用程序再次回到同一activity,但它得到它在其他語言設定和調用OnResume()改變編程語言不應用強制關閉

我的問題是如何讓我的應用程序迴歸到同一Language config即使我的應用程序已經崩潰的某些原因。

我試圖刷新並獲得最後的配置在sharedPreferences設置和刷新我的配置或內容的看法,但沒有運氣

代碼:

@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); 
} 

回答

0

試試這個:

你可以捕捉到碰撞事件並將您的activity加載到您想要的localeconfiguration ...

使用這個工具類:

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); 
    } 
} 

現在,在您MainactivityonCreate

Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); 
+0

我的問題是不是在SharedPreferences使用它,它爲我工作,但得到的右侍郎之後,佈局仍然工作在其他的語言我的應用程序後墜毀 –

+0

應用得到了墜毀後...共享偏好失去了它的價值讓你'appSharedPrefs.getBoolean'爲您提供了錯誤的值 – rafsanahmad007

+0

看到這一點:http://stackoverflow.com/questions/15353900/shared -preferences-reset-data-when-app-is-force-closed-or-device-is-restarted – rafsanahmad007