简体   繁体   中英

Update attachBaseContext in MainActivity.java from a fragment

How can I update attachBaseContext in MainActivity from a fragment? Eg

MyFragment.java

String chosenLanguage = "en";

public void updateLanguage(String chosenLanguage) {
    //...Update MyContextWrapper.wrap(newBase, chosenLanguage) here
}

MainActivity.java (Following succesfully calls a java class and changes app language)

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(MyContextWrapper.wrap(newBase,"en"));
}

You need to restart activity to run 'attachBaseContext' again.

In your fragment run:

Intent intent= new Intent(getContext(), MainActivity.class); startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM