How to hide action bar before activity is created,

2019-01-01 12:18发布

I need to implement splash screen in my honeycomb app. I use this code in activity's onCreate to show splash:

setContentView(R.layout.splash);
getActionBar().hide();

and this code to show main UI after sometime:

setContentView(R.layout.main);
getActionBar().show();

But before onCreate is called and splash appears, there is small amount of time when action bar shown.

How can I make action bar invisible?

I tried to apply theme to activity without action bar:

<item name="android:windowActionBar">false</item>

but in that case getActionBar() always returns null and I found no way to show it again.

25条回答
有味是清欢
2楼-- · 2019-01-01 12:19

Actually, you could simply set splash Activity with NoActionBar and set your main activity with action bar.

查看更多
不再属于我。
3楼-- · 2019-01-01 12:20

Put your splash screen in a separate activity and use startActivityForResult from your main activity's onCreate method to display it. This works because, according to the docs:

As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be displayed until a result is returned back from the started activity. This is to avoid visible flickering when redirecting to another activity.

You should probably do this only if the argument to onCreate is null (indicating a fresh launch of your activity, as opposed to a restart due to a configuration change).

查看更多
像晚风撩人
4楼-- · 2019-01-01 12:20

Just add this in your styles.xml

<item name="android:windowNoTitle">true</item>
查看更多
残风、尘缘若梦
5楼-- · 2019-01-01 12:21

you can use this :

getSupportActionBar().hide(); if it doesn't work try this one :

getActionBar().hide();

if above doesn't work try like this :

in your directory = res/values/style.xml , open style.xml -> there is attribute parent change to parent="Theme.AppCompat.Light.DarkActionBar"

if all of it doesn't work too. i don't know anymore. but for me it works.

查看更多
人间绝色
6楼-- · 2019-01-01 12:22

this may be handy
add this to your manifest

 android:theme="@android:style/Theme.Light.NoTitleBar" 

cheers

查看更多
无与为乐者.
7楼-- · 2019-01-01 12:22

I had still error with null pointer and finally it helped when I called first getWindow().requestFeature() and then super.onCreate()

public void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
getActionBar().show();
查看更多
登录 后发表回答