Android 7.0中Fragment切换时Toolbar与Tablayout无法隐藏问题求助
Hey there, let's figure out why your Toolbar and TabLayout won't hide on Android 7.0 when switching away from your main Fragment—this is a common quirk with older Android versions, but we can fix it!
First, let's recap your issue: Normally, your Toolbar and TabLayout hide when you leave the main Fragment and reappear when you come back. But on Android 7.0, they stay stuck on top of every Fragment, even after you tried using getActivity().findViewById() to toggle their visibility.
Here are the most likely fixes, ordered by how often they solve this problem:
1. Use ActionBar Methods Instead of Direct View Visibility (If You're Using Toolbar as ActionBar)
If you set your Toolbar as the Activity's ActionBar with setSupportActionBar(toolbar), directly calling setVisibility(View.GONE) might not work reliably on Android 7.0. The system might override the view state to keep the ActionBar visible. Instead, use the ActionBar's built-in methods:
In your main Fragment (to show):
@Override public void onResume() { super.onResume(); // Show ActionBar if (getActivity() != null && getActivity().getSupportActionBar() != null) { getActivity().getSupportActionBar().show(); } // Show TabLayout TabLayout tabs = getActivity().findViewById(R.id.tabs); if (tabs != null) { tabs.setVisibility(View.VISIBLE); } }
In other Fragments (to hide):
@Override public void onResume() { super.onResume(); // Hide ActionBar if (getActivity() != null && getActivity().getSupportActionBar() != null) { getActivity().getSupportActionBar().hide(); } // Hide TabLayout TabLayout tabs = getActivity().findViewById(R.id.tabs); if (tabs != null) { tabs.setVisibility(View.GONE); } }
2. Ensure Fragment Lifecycle Methods Are Actually Triggered
Android 7.0 handles Fragment lifecycle callbacks a bit differently, especially if you don't use addToBackStack() when switching Fragments. If you skip adding to the back stack, returning to the main Fragment might not trigger onResume(), so your visibility code never runs.
Make sure your Fragment transaction includes addToBackStack():
getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, new OtherFragment()) .addToBackStack(null) // This is crucial! .commit();
3. Cache Your View References to Avoid Null Issues
Calling getActivity().findViewById() every time can lead to null references on older Android versions, especially if the Activity's view hasn't fully initialized. Cache the Toolbar and TabLayout references in your Fragments' onActivityCreated() method:
private Toolbar mToolbar; private TabLayout mTabs; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (getActivity() != null) { mToolbar = getActivity().findViewById(R.id.toolbar); mTabs = getActivity().findViewById(R.id.tabs); } }
Then use these cached references in onResume() instead of calling findViewById() each time.
4. Check Layout Hierarchy & fitsSystemWindows
Android 7.0 has stricter rules for layout hierarchy, especially if you're using android:fitsSystemWindows="true" on your Toolbar or root layout. This attribute can cause views to stick to the top of the screen even when you set them to GONE.
Try removing fitsSystemWindows="true" from your Toolbar's parent layout, or adjust the hierarchy so your Fragment container is drawn on top of the Toolbar/TabLayout (if that's what you intend).
Quick Debug Tip
If none of the above works, add log statements in your visibility code to check:
- Is the code actually running (are the lifecycle methods being called)?
- Are
mToolbarandmTabsnull when you try to toggle visibility?
This will help you narrow down whether the issue is with the visibility logic itself, or with the view references/lifecycle.
内容的提问来源于stack exchange,提问作者Mr.BitCoin

