4

I am working on I18N application which works for 6 countries, In have a requirement like, when the user changes the language from the settings, the application should be restart itself and display the app in changed language. for that I am storing the language value in shared preference and comparing with the current language value. if the value is not same I need to re-launch the application. For this I am using below code:

if (currentLanguage == null) {
    Util.saveStringInSP(_activity, "LANGUAGE", languageValue);
}
else if (currentLanguage.equalsIgnoreCase(languageValue)) {
    String currentLanguage = Util.getStringFromSP(_activity, "LANGUAGE");
}
else {
    try {
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    catch(Exception e) {   }
    Intent i = new Intent();
    PackageManager manager = getPackageManager();
    i = manager.getLaunchIntentForPackage("com.pfizer.stablemate");
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
}

Please provide me a piece of code to relaunch the Android application programmatically.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ganesh
  • 662
  • 3
  • 8
  • 26
  • 4
    You don't need to kill/restart for that. you just need to **not** have *locale* in you configChanges field in the manifest, and android will restart your activity for you. – njzk2 Dec 19 '12 at 15:51
  • I don't want to restart the activity, I need to restart the entire application from the starting. – Ganesh Dec 20 '12 at 04:43
  • then you can actually **have** locale in your configChanges field, and receive the locale change in onConfigChange in your activity – njzk2 Dec 20 '12 at 09:22

1 Answers1

8

Try :

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(        
getBaseContext().getPackageName() );
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Kooki
  • 1,157
  • 9
  • 30