'How to hide actionbar in Activity, but show in fragment

i have a fragment from an activity, i hide the actionbar in my activity like so supportActionBar?.hide() in the onCreate function of the activity when the app first starts. When the fragment is created, i have the following code in the onCreate to show the actionbar in the fragment:

activity?.supportActionBar?.show()
activity?.supportActionBar?.setTitle(R.string.host)

The problem is, that when i go back to the main Activity, the actionbar is now there. Is there a way to permanently keep the actionbar disabled for the main Activity, but have it showing for the fragments?



Solution 1:[1]

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.YourAppName" parent="Theme.MaterialComponents.NoActionBar.Bridge">
        <!-- Primary brand color. -->
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
    
        <!-- Customize your theme here. -->
    </style>
</resources>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 K.Mat