How to restart an Activity on Android (Java)

How to restart an Activity on Android (Java)

Using the function bellow in a activity class you can restart the current activity.

private void restartActivity() {
   Intent intent = getIntent();
   finish();
   startActivity(intent);
}

Why did you need to restart an activity? Well personally I need it after I had change the current Locale – in order to change the app language. Changing the locale to another language and forcing the activity to restart it olso force it to load the layout and string values from the new language folder, in this way I did implement a language changing option.

No comments.