Re-launch android application programmatically

Ganesh picture Ganesh · Dec 19, 2012 · Viewed 9.2k times · Source

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.

Answer

Kooki picture Kooki · Dec 19, 2012

Try :

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(        
getBaseContext().getPackageName() );
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);