Hide the action bar in fragmentation mode

I am creating a framework for an application and want to hide the action bar for login activity. The application will consist entirely of fragments because they are awesome.

I can hide the action bar perfectly for my activity, but every time I launch the application, I see my bloated empty fragment and the default action bar for about 2 seconds before it can inflate the input fragment.

Any idea on how / where should I hide the action bar (keeping my structure) in order to stop this?

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 a SingleFragmentActivity, extending FragmentActivity and did this in the if statement:

  requestWindowFeature(Window.FEATURE_NO_TITLE); 

But I have the feeling that just another way to do the same

Edit:

I also tried setting a different style for the fragment_activity parameter, but it doesn't work either:

  <style name="NoActionBar" parent="Widget.AppCompat.ActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> </style> 
+6
source share
4 answers

Well, after a few hours, I think I found a solution to save fragments for everything and remove the action bar (for all devices) for launch 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> 

If the style is defined like this:

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

It took some time to understand, because there is no Theme.AppCompat.Light.NoActionBar, the action bar is mostly known as the title bar in the old APIs, and when I applied this style to the activity itself (which I thought was the only way to use dynamic themes), it seems like it doesn't work at all.

So hopefully this simple solution will contain an action bar for any other actions, and this may help someone in the future :)

+14
source

try the following:

 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(); } 
+11
source

Daniel Wilson's comment above, but only a slight difference in how I did it. I did not define the style at all, all I did in AndroidManifext.xml is that I added this to my activity:

 android:theme="@style/Theme.AppCompat.NoActionBar" 

So, I didn't have to redefine the style for NoActionBar, I just added it from Theme.AppCompat

+3
source

I use this code in Activity.onCreate ()

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Hiding the title bar has to happen before the view is created requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); // ... } 
+1
source

Source: https://habr.com/ru/post/958608/


All Articles