Android - getActionBar() returns null after upgrading to SDK 21

I was working on a new app in Android Studio Beta with target SDK 20. It was all well and good, but after updating target SDK version to 21 (and appcompat-v7 to 21.0.0) the app started crashing on launch.

Quick debugging revealed that the issue was cased by the line of code actionBar = getActionBar(); inside main activity onCreate method. getActionBar() was now returning NULL.

After some digging I resolved the issue by changing application theme from Theme.AppCompat to android:Theme.Holo. This will only work if your minimum SDK is 14 or higher, which was the case in this instance.

Read more: Android - getActionBar() returns null after upgrading to SDK 21

Android - hiding ActionBar icon and title during launch

I was developing an app that had a tabbed interface (FragmentPagerAdapter) inside the main activity. Each tab was setting different ActionBar icon and title inside onTabSelected method. Something like this:


@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
   viewPager.setCurrentItem(tab.getPosition());
   if (tab.getPosition() == 0){
      actionBar.setTitle("title1");
      actionBar.setIcon(R.drawable.icon1);
   }
   if (tab.getPosition() == 1) {
      actionBar.setTitle("title2");
      actionBar.setIcon(R.drawable.icon2);
   }
   if (tab.getPosition() == 2) {
   actionBar.setTitle("title3");
   actionBar.setIcon(R.drawable.icon3);
}
}

This was working fine when the app was running and resuming from paused. But on the initial launch, global application icon and title was appearing inside ActionBar for half a second or so, before being overridden by the code in onTabSelected .

Read more: Android - hiding ActionBar icon and title during launch