Android:更改语言后刷新当前片段 [英] Android: Refresh current fragment after language change

查看:833
本文介绍了Android:更改语言后刷新当前片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的MainActivity有一个带有一组片段的导航抽屉.

So my MainActivity has a navigation drawer with a set of fragments.

在导航栏的顶部,我有2个标志代表一种语言.如果用户单击一种语言,则该应用程序会更改整个应用程序的语言.

On top of the nav bar, I have 2 flags which represent a language. If an user clicks on a language, the app changes the language of the whole app.

一切正常(活动刷新,导航抽屉和所有片段都已翻译),但打开的片段却无法正常工作.这意味着用户需要单击导航抽屉,然后再次选择片段以查看其翻译.

Everything is working smoothly (activity refreshes, navigation drawer and all fragments get translated) but the fragment that's open doesn't. This means that the user needs to click on the navigation drawer and select the fragment again to see it translated.

如何刷新用户所在的当前片段?

How can I refresh the current fragment the user is in?

推荐答案

在提交片段时将标签添加到您的片段中:

Add tag to your fragment when you commit it:

fragmentManager.beginTransaction().replace(R.id.your_id, fragment, "Your_Fragment_TAG").commitAllowingStateLoss();

然后,您可以分离并重新附加片段以进行刷新:

Then You can detach and attach again your fragment for refresh:

// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

这篇关于Android:更改语言后刷新当前片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆