Reputation: 6083
In my app's preferences I have an option to change app's language.
public class Fragment_Preferences extends PreferenceFragment {
private SharedPreferences.OnSharedPreferenceChangeListener prefListener;
SharedPreferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.equals("language_preference"))
{
// Set language change flag to true -
// the Main Fragment will be recreate when this fragment finishes and the main restarts
Common_Methods.set_locale_changed(true);
}
}
};
prefs.registerOnSharedPreferenceChangeListener(prefListener);
}
}
So when I change the language, the preference fragment doesn't change it's language immediately. I have to exit preferences, then in my Fragment or Activity (depends from where I called for Preference fragment) I have this code, which restarts the current fragment or activity with new language settings:
public void onConfigurationChanged(Configuration newConfig) {
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
{
finish();
startActivity(getIntent());
} else {recreate();}
}
}, 1);
Common_Methods.set_locale_changed(false); // Reset the Language change flag to prevent repeating Fragment recreation.
super.onConfigurationChanged(newConfig);
}
If I reopen Preference fragment at that point - it will be in the new selected language.
I tried to copy the above method to my Preference_Fragment, but I'm getting errors. So the question is: how can I recreate/reload the Preference_Fragment with the new language immediately after it was selected, without having to exit the fragment first?
Upvotes: 0
Views: 2053
Reputation: 666
Floren's answer didn't work for me (maybe due to using the Android Support Library), but it lead me to the following solution based on the same basic idea from Florens. The difference is removing and adding the fragment instead of replacing:
In your activity:
public void restartFragment() {
FragmentManager fm = getSupportFragmentManager();
if (fragment != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
fragment = <new instance>;
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentActivity, main, "Main");
ft.commit();
}
In your fragment:
((<your activity>)getActivity()).restartFragment();
Upvotes: 0
Reputation: 43
It is best if you let the activity know (through getActivity() and casting) that you want your fragment updated, and add logic to the activity to remove the fragment and add a new instance.
In your settings activity:
public void restartFragment() {
SettingsFragment fragment = new SettingsFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
}
And in your settings fragment:
((SettingsActivity) getActivity()).restartFragment();
Upvotes: 2