Search code examples
androidpopuprestart

Pop-up that will restart the App Android


Hi Everyone i'm trying to create a function on my app that will change the language ( on Android ) the thing is that it does not really work well, thus i have to restart my App in order for the language change to be applied

What i am trying to achieve is to first select the language a pop up and then to inform the user that the app will be restarted in order for the change to be applied .

here is the piece of code i'm using to change the language and for the app to save the change language when it restart:

public class LocalizationUpdaterActivity extends Activity {

private String[] languages = { "English", "Francais", "Espanol", "Ivrit" };
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_langues);

    SharedPreferences sp = this.getApplicationContext().getSharedPreferences("loginSaved", Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sp.edit();


    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    spinner.setPrompt("select language");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, languages);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView arg0, View arg1,
                                   int arg2, long arg3) {
            Configuration config = new Configuration();
            switch (arg2) {
                case 0:
                    config.locale = Locale.ENGLISH;
                    editor.putString("Langues", "en_US");
                    break;
                case 1:
                    config.locale = Locale.FRENCH;
                    editor.putString("Langues", "fr_FR");
                    break;
                case 2:
                    config.locale = new Locale("es_ES");
                    editor.putString("Langues", "es_ES");
                    break;
                case 3:
                    config.locale = new Locale("he", "IL");
                    editor.putString("Langues", "he_IL");
                    break;
                default:
                    config.locale = Locale.ENGLISH;
                    editor.putString("Langues", "en_US");
                    break;
            }
            getResources().updateConfiguration(config, null);
        }

        public void onNothingSelected(AdapterView arg0) {
            // TODO Auto-generated method stub

        }
    });
}

}


Solution

  • I had the same issue as you're currently faced with.
    To accomplish this, i implemented a BroadcastReceiver in my BaseActivity (which all activities extend from), which when receiving a specific command, would finish the activity.
    This would finish all activities when the command is sent, thus killing the application.

    To restart it immediately, you simply create an intent for the wanted activity and start the activity in the same function as you're killing the application.

    A simple example is shown below:

    public abstract class BaseActivity extends FragmentActivity {
    
        private KillReceiver killReceiver;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            killReceiver = new KillReceiver();
            registerReceiver(killReceiver, IntentFilter.create("kill", "content://all"));
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            unregisterReceiver(killReceiver);
        }
    
        private final class KillReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                finish();
            }
        }
    }
    

    And these 2 functions can be used to kill/restart the application.

    public void killApplication(Activity activity) {
        //Broadcast the command to kill all activities
        Intent intent = new Intent("kill");
        intent.setType("content://all");
        activity.sendBroadcast(intent);
    }
    
    public void restartApplication(Activity activity) {
        killApplication(activity);
    
        //Start the launch activity
        Intent i = activity.getBaseContext().getPackageManager().getLaunchIntentForPackage(activity.getBaseContext().getPackageName());
        activity.startActivity(i);
    }
    

    This way of doing it has never failed me, and i haven't found any issues with it.