ActionBarsherlock android.R.id.home <API 11 - android

When i used Actionbarsherlock
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I notice that android.R.id.home is from API 11.
How can we make sure android.R.id.home is right on API 8?

It is a static final constant which means its value is copied into the compiled code instead of being just a reference. This is why it is able to be used successfully on every API level.

Related

simulate up when back button is pressed

I'm working on an android app and recently, I discovered that the default back button onBackPressed() produces a back behaviour, while the getSupportActionBar().setDisplayHomeAsUpEnabled(true) produces an up behaviour. And the two have significant differences.
I was wondering if I can simulate the up behaviour when I press the hardware back button, so that it navigates up instead of back.
Thanks.
You can do this.
#Override
public void onBackPressed() {
NavUtils.navigateUpFromSameTask(this);
}
You can do this
Overide onOptionsItems Selected
Create a switch case of android.R.id.home
Call method onBackPressed();
here is the code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}

Endless loop on Up button in Settings Activity - large tablets only

I'm having problems with SettingsActivity which was generated by Android Studio. Specifically pressing the Up button creates a new instance of settings activity instead of going back to the parent activity. This happens only on large tablets where settings are split - i.e. headers to the left of the list of settings.
The following code was generated by Android Studio:
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
On large tablets onMenuItemSelected() always returns true so navigateUpFromSameTask() is never called. However, even forcing the call to navigateUpFromSameTask() produces the same results.
Example:
...
if (!super.onMenuItemSelected(featureId, item) ||
isXLargeTablet(this)) {
NavUtils.navigateUpFromSameTask(this);
}
...
The only thing that seems to work is a call to finish().
Example:
if (id == android.R.id.home) {
if (isXLargeTablet(this)) {
finish();
return true;
}
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
The parent activity's (main activity) launchMode is set to "singleTop" in the manifest.
The same navigation works fine on smaller tablets and phones.
I'm not sure if my hacky solution is sufficient.
Is there another way to deal with this 'bug'?
Thanks.

Action bar with written clickable?

I would like to have an ActionBar in Android similar to evernote where in the top right there is a sign that if touched triggers an action. How do I put this in writing clickable? And as I move to the right?
You need to override OnOptionsItemSelected() method in your activity. You can also check the for offical doc for full tutorial
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuItem1:
action1();
case R.id.menuItem2:
action2();
default:
return super.onOptionsItemSelected(item);
}
Good luck

I am trying to setup "action bar's Up/Home button" on my other activities but I keep getting an error "NavUtils". How can I solve this?

// this is code on my other activities
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
//NavUtil show error
//suggests creating another class
return true;
}
return super.onOptionsItemSelected(item);
}
}
Make sure you have latest Android.support.v4.jar added to your lib files.
Also there are a few known issues with this files. See the link below:
Download Android Support v4 Jar Revision 9 (!)

SherlockActionBar up button won't work on API 10

I'm playing around with the sherlockactionbar library, there is a problem which is the up button won't work on API 10. The up icon can be shown on the actionbar, but it won't respond to my pressing. On higher API such as 16, it functions as expected without problems.
I tried several solutions I came across, but none of them work. Below is one of them.
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpTo(this, new Intent(this, test.class));
return true;
Make sure you have these set:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
And that your switch is in this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
/* Do Something //*/
return true;
default:
return false;
}
}

Categories

Resources