Hide Action Bar in a Fragment Activity

android, android-actionbar-compat, android-actionbaractivity, android-fragmentactivity

Solution

Okay after a few hours I think I have found the solution to keeping fragments in place for everything and removing the action bar (for all devices) for the launch activity only:

Android Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.app"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.company.app.LoginActivity"
        android:theme="@style/NoActionBar"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Where the style is defined as like so:

   <style name="NoActionBar" parent="Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
</style>

That took a while to figure out because there is no Theme.AppCompat.Light.NoActionBar , the action bar is basically known as the title bar in older APIs, and when I applied that style to the activity itself (which I thought was the only way of using dynamic themes) it doesn't seem to work at all.

So hopefully this simple solution will keep the action bar for any other activities and it may help someone in the future :)

Problem

I am creating the framework for an app and wish to hide the action bar for the login activity. The app is going to be entirely composed of fragments because they are awesome. I can hide the action bar fine for my activity, but every time I run the app I see my inflated blank fragment and default action bar for about 2 seconds before it manages to inflate the login fragment. Any idea on how / where I should hide the action bar, (whilst keeping my framework) to stop this from happening? LoginActivity.java ``` public class LoginActivity extends SingleFragmentActivity { @Override protected Fragment createFragment() { return new LoginFragment(); } ``` } SingleFragmentActivity.java ``` public abstract class SingleFragmentActivity extends ActionBarActivity { protected abstract Fragment createFragment(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(this instanceof LoginActivity){ ActionBar actionBar = getSupportActionBar(); actionBar.hide(); } setContentView(R.layout.activity_fragment); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = createFragment(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } ``` } LoginFragment.java ``` public class LoginFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_login, parent, false); return v; } ``` } I originally had SingleFragmentActivity extend FragmentActivity and did this in the if statement: ``` requestWindowFeature(Window.FEATURE_NO_TITLE); ``` But I've got a feeling that's just a different way of doing the same thing Edit: I have also tried setting the fragment_activity to use a different style but it doesn't seem to work either: ``` <style name="NoActionBar" parent="Widget.AppCompat.ActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> </style> ```

Original source