Refresh(recreate) the activities in back stack when change locale at run time


Refresh(recreate) the activities in back stack when change locale at run time



I have an Activity say ActivityMain from this activity I moved to another activity called ActivitySettings and in settings activity I'm changing the App locale by clicking on a button, and using recreate I achieved the change I need in current activity but when I press back my `ActivityMain' will resume but locale is not updated.


ActivityMain


ActivitySettings



Can some one tell me how to 'Recreate' backstack activities? what will be the correct approach.



I can't call recreate on refresh as it will be infinite loop





Look at here developer.android.com/training/basics/intents/result
– VolkanSahin45
2 days ago




1 Answer
1



In each Activity's onCreate() you can maintain the currentLangCode. Check this value in onResume(), if it differs, you can conclude the locale was change and recreate()


onCreate()


currentLangCode


onResume()


recreate()



You can do it as follows:


public class ActivityA extends AppCompatActivity{
private String currentLangCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
currentLangCode = getResources().getConfiguration().locale.getLanguage();
...
}
@Override
public void onResume(){
...
if(!currentLangCode.equals(getResources().getConfiguration().locale.getLanguage())){
currentLangCode = getResources().getConfiguration().locale.getLanguage();
recreate();
}
}
...
}



If you want to apply it for all the Activities, then simply create BaseActivity as follows:


public class BaseActivity extends AppCompatActivity{
private String currentLangCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
currentLangCode = getResources().getConfiguration().locale.getLanguage();
...
}
@Override
public void onResume(){
...
if(!currentLangCode.equals(getResources().getConfiguration().locale.getLanguage();)){
currentLangCode = getResources().getConfiguration().locale.getLanguage();
recreate();
}
}
...
}



Extend all Activities from BaseActivity


BaseActivity


public class ActivityA extends BaseActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
@Override
public void onResume(){
super.onResume();
}
...
}





thank you for the answer, let me try and update.
– Praneeth
2 days ago





Sure. Let me know.
– Sagar
2 days ago





getResources().getConfiguration().locale.getLanguage() this this is not giving changed language it is still giving me old lang and the condition is always flase I made a work around by checking with shareprefs where i'm saving the changed language
– Praneeth
2 days ago





Thanks for update
– Sagar
2 days ago





@Praneeth, just tested the value, its returning correct value for me.
– Sagar
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV