Android Fragment两类问题:语言切换失效与ViewPager返回空白
Hey there, let's break down and fix these two Fragment-related problems you're facing—they're both pretty common once you start working with nested Fragments and localization!
1. Fragment Won't Switch Language from English (en) to Chinese (zh)
This usually happens because the Fragment isn't picking up the updated Configuration after the language change. Here are the fixes to try:
Recreate the Host Component After Language Switch
When you change the app's locale, the Activity's Configuration gets updated, but existing Fragments might not automatically refresh. After applying the new locale, callrecreate()on the host Activity, or replace the Fragment with a new instance to force it to load the updated resources.Avoid Caching String Resources
Looking at your code, you're usinggetString(R.string.home_nav)when adding Fragments to the ViewPager. If you cached this string early (like inonCreate), it'll stay in the original language. Instead, fetch the string dynamically whenever you need it (e.g., inonResumeor when updating the ViewPager adapter) to get the latest localized value.Ensure Proper Locale Configuration Setup
Double-check your language switching code to make sure it's correctly updating the app's Configuration. For AndroidX apps, use the recommended method to set locales:LocaleListCompat localeList = new LocaleListCompat(new Locale("zh")); AppCompatDelegate.setApplicationLocales(localeList); // Recreate the activity to apply changes requireActivity().recreate();For older Android versions, make sure you're updating the Resources and Configuration properly, and that the changes are applied across the app.
2. ViewPager in Fragment Shows Blank Page When Returning, But Back Key Works
The issue here is almost certainly related to the FragmentManager you're using for the ViewPager adapter. Let's fix it:
Use
getChildFragmentManager()Instead of Activity's FragmentManager
In your code, you're initializing the adapter withgetActivity().getSupportFragmentManager(). When you use the Activity's FragmentManager for nested Fragments (the ones inside the ViewPager), their lifecycle isn't properly bound to the parent Fragment. When you navigate back, the parent Fragment is restored, but the nested Fragments don't get reattached correctly.Update your setup code to use the parent Fragment's own child FragmentManager:
private void setupViewPager(ViewPager viewPager) { // Replace getActivity().getSupportFragmentManager() with getChildFragmentManager() adapter = new ViewPagerAdapter(getChildFragmentManager()); adapter.addFragment(new TimelineFragment(), getString(R.string.home_nav)); adapter.addFragment(new DiscoverNewFragment(...)); // Add other fragments as needed viewPager.setAdapter(adapter); }Handle ViewPager State Properly
Make sure you're initializing the ViewPager and adapter in the right lifecycle method (likeonViewCreatedinstead ofonCreate) and that you're not reinitializing the adapter unnecessarily when the Fragment is restored. For example:@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ViewPager viewPager = view.findViewById(R.id.your_view_pager_id); if (adapter == null) { setupViewPager(viewPager); } else { viewPager.setAdapter(adapter); } }Check Nested Fragment Layouts
Ensure your child Fragments (TimelineFragment, DiscoverNewFragment, etc.) are inflating their layouts correctly inonCreateView—avoid caching the root View unless you're properly handling state restoration, as this can lead to blank views when the Fragment is recreated.
内容的提问来源于stack exchange,提问作者Aruttan Aru

