Something about ActionBarActivity


#1

First of all, thank the authors of this book, let me have a detailed understanding of many fuzzy places.

I want to share some ideas after reading this chapter.

After the ADT upgraded to 22.6.0(and Gradle to 2.0), the default activity your new activity extending is “ActionBarActivity”. I once didn’t understand very well what the “ActionBar” means. After reading this chapter, I have known that this activity is contained in the android.support.v7.app package and one of its important effect is to make up for the loss of ActionBar before SDK 11. The code can be more clear with this support library. You don’t need to use annotation @TargetApi(11) and if condition to determine the SDK either.
when your activity is extending ActionBarActivity, the code in list 16-8 can be changed from

@TargetApi(11) @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, parent, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); } ... }

to

@Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, parent, false); ((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); ... }

which can be more readable. I have tested it on devices with SDK 2.2 and SDK 4.1, both run well.

Before I have tryed this change, I have the buggy code getActivity().getActionBar() with null exception because my activity is extending ActionBarActivity. They are not compatible. When I search the solutions of this problem on statckoverflow, the result is to change theme to “theme.holo.light”, which is after SDK 11, but I know it’s not the answer I wanted. I believe theme.appcompat.light has a good support for the SDK before SDK 11. After finding the method getSupportActionBar(), I know it’s the final key.

I also want to discuss the method notifyDataSetChanged().

After testing code on devices with SDK 2.2 and SDK 4.1, I found when using back button, there is no need to use this method on device with SDK 4.1, which will refresh the data automatically, but on device with SDK 2.2, this method is necessary. When I use the Ancestral Navigation(HomeAsUp Button), this method is redundant. Both devices with SDK 2.2 and SDK 4.1 will run well without this method.

Finally, I have changed my IDE from Eclipse to Android Studio. I wish the next version of this book could use the Android Studio as the IDE and introduce some more unique tools on this development platform

Thank the authors once again.


#2

Your post fixed my issue of NullPointException. It works fine on Android 5.0 (Nexus 4). Many thanks!


#3

Hi Guys,

I’m getting the same nullPointerException, however when I extent CrimeFragment to ActionBarActivity i get issues with the onCreate function, can anyone help?

Paul


#4

[quote=“paulprior”]Hi Guys,

I’m getting the same nullPointerException, however when I extent CrimeFragment to ActionBarActivity i get issues with the onCreate function, can anyone help?

Paul[/quote]

Paul,

I had this same problem. I left CrimeFragment as a ListFragment. Instead, I cast getActivity() as an ActionBarActivity.

//getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

The same goes for the get and set subtitle calls in CrimeListFragment.

Hope this helps.


#5

Absolutely appreciate the help, but unfortunately that did not work. I changed my CrimeFragment to extend listFragment and casted to ActionbarActivity. Got a ClassCastExcption: getActivity cannot be cast to android.support.c7.app.ActionBarActivity.

Just an an FYI: the only way I could get an actionbar displaying full stop was to extent SingleFragmentActivity by ActionBarActivity, and it appears with no icon. Yes the icon is stipulated in the Manifest file. I’ve tried all the Stack Overflow solutions to this problem, none of them work and I’m using an ActionBar compatable theme.

Any ideas?


#6

CrimeFragment should not extend ListFragment. Only CrimePagerActivity and SingleFragment should extend ActionBarActivity.