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>
If we want to remove the ActionBar only from specific activities, we can create a child theme with the AppTheme as it's parent, set windowActionBar to false and windowNoTitle to true and then apply this theme on an activity level by using the android:theme attribute in the AndroidManifest. xml file.
If you are using AppCompatActivity (you should) then this is the solution that worked for me: ((AppCompatActivity) getActivity()). getSupportActionBar(). hide();
If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme.
How do we hide the menu on toolbar in one fragment? When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment.
If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles.xml and change the base application to “ Theme.AppCompat.Light.NoActionBar “. 2. Hide ActionBar from any particular activity using Java code
When the app bar is owned by an activity, fragments can interact with the app bar by overriding framework methods that are called during fragment creation. Note: This guidance applies only when the app bar is owned by the activity. If your app bar is a toolbar that's included in a fragment layout, see Fragment-owned app bar.
If most screens in your app don't need an app bar, or if perhaps one screen needs a dramatically-different app bar, you can add a Toolbar to your fragment layout. Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen.
When your app consists of many modules and each module contains several fragments, you will need to hide or show menu items depending on the showing fragments. Each module’s menu items should be put in a group for easy access. All groups’ visible states are set to false by default.
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 :)
try this:
if(this instanceof LoginActivity) {
if (getActionBar().isShowing()) getActionBar().hide();
} else {
if (getActionBar().isShowing()) getActionBar().hide();
}
or
if(this instanceof LoginActivity){
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
} else {
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With