I am using ActionBarSherlock to implement ActionBar for both Pre/post versions of Android HoneyComb.
My problem is that when I tap top-left icon on Android version 4.0.4, it did not respond.
Here is what I have done till now:
1) In all style folders “values/styles.xml” , “values-v11/styles.xml” & “values-v14/styles.xml”; I have done the following
<style name="ActivityTheme" parent="@style/AppTheme">
<item name="actionBarStyle">@style/ActivityActionBarStyle</item>
<item name="android:actionBarStyle">@style/ActivityActionBarStyle</item>
</style>
<style name="ActivityActionBarStyle" parent="ommaralrd_transparent_ActionBar">
<item name="displayOptions">showHome|showTitle|homeAsUp</item>
<item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
</style>
In any application activity (except Home activity since it should not have Up arrow), I have done the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inner);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
.....rest of my code ...
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case android.R.id.home:
/*app icon in action bar clicked; go home*/
intent = new Intent(this, MainActivity.class);
/* With this flag, if the activity you're starting already exists in the current task,
* then all activities on top of it are destroyed and it is brought to the front.
* you should usually not create a new instance of the home activity. Otherwise,
* you might end up with a long stack of activities in the current task with multiple
* instances of the home activity.*/
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
In the Manifest file, I make sure that I have applied the respective style to all activities (except main activity since it should not have Up arrow)
<activity
android:name="com.andrid.example.TestActivity"
android:label="@string/app_name"
android:theme="@style/ActivityTheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.andrid.example.MainActivity" />
</activity>
So now when I test the application on Pre-HoneyComb version, the Up arrow is never shown which is correct since ABS can’t respond at all if icon was tapped to navigate up.
But when I tried the application on Post-HoneyComb version like 4.1 on Emulator, the Up arrow is shown and when I tapped it, it works as expected and navigates up normally.
My problem is that when I tried the application on Android 4.0.4 emulator , the Up arrow is shown as expected but when I tapped it, it do nothing
I have found the fix:
Simply I should avoid set the Icon Home as Up using xml attribute: homeAsUp but instead I should have used setDisplayHomeAsUpEnabled(true) explicitly as follows:
In all style folders “values/styles.xml” , “values-v11/styles.xml” & “values-v14/styles.xml”; I have done the following
in Each Activity (except main activity):