Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-launch android application programmatically

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.

like image 352
Ganesh Avatar asked Jan 14 '23 17:01

Ganesh


1 Answers

Try :

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(        
getBaseContext().getPackageName() );
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
like image 145
Kooki Avatar answered Jan 20 '23 16:01

Kooki