I am writing an app for android (2.1 > 3.1) and I would like to use the familiar practice of using the app Icon in Honeycomb apps to go up to the home activity, however, when I run the activity on earlier, non Honeycomb devices where the Activity.getActionBar(); method does not exist yet, the app force closes, how can I only run this specified code if the device is running honeycomb?
@Override
protected void onStart() {
super.onStart();
ActionBar actionBar = this.getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
Thanks for any help and have a great day.
Since the actionbar dont exist on pre honeycomb you'll have to make do with something else. One suggestion would be to use johannilssons actionbar library which can be found on github. Direct link: https://github.com/johannilsson/android-actionbar
As of Revision 18 the Android Support library contains ActionBar support back to API Level 7. This is now the recommended way to support the ActionBar for all versions of Android from 2.1 up and is significantly easier to use than third party libraries or other hacks.
I think the code is self-explanatory
From the API Guide for the Action Bar it says:
You can get this by installing the Android 4.1 (API 16) samples.
Then in Eclipse:
Android pre-Honeycomb doesn't have an ActionBar, so any method concerning the actionBar will just fail. You should take a look at the code from the Google IO app, which uses an ActionBar both for Honeycomb and pre-Honeycomb.
Put simply, it won't work by itself, you'll have to include your own ActionBar code.
I like to use GreenDroids action bar (plus they include some other pretty things): http://android.cyrilmottier.com/?p=240