Evaluating R.id.home - android

I was following this tutorial here - http://developer.android.com/training/implementing-navigation/ancestral.html - for implementing the Up Navigation. Sort of the same as the user pressing the Back button on the phone, but the onBackPressed() method does not fire when the 'Up' button is pressed. In the tutorial they show that you trap R.id.home in the onOptionsItemSelected() method. This web page - http://developer.android.com/reference/android/R.id.html - shows that the value of R.id.home should equal 16908332, but it doesn't in my app. In the code below if I use R.id.home it fails. If I hard-code in 16908332 it works. For me R.id.home evaluates to 21312330724. According to the page, all of the R.id values begin 1690. I hate hard-coding in a value for a built-in value, but I'm not sure what else to do. Could this cause problems down the road? Am I doing something wrong? Is this a bug?
Greg
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.home) {//16908332
Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.putExtra(CAT_ID, CatID);
upIntent.putExtra(USER_ID, UserID);
upIntent.putExtra(LIST_ID, ListID);
setResult(RESULT_OK, upIntent);
NavUtils.navigateUpTo(this, upIntent);
return true;
}
return super.onOptionsItemSelected(item);
}

You need to compare it to the android one:
if (id == android.R.id.home){
...
}

Related

navigation view android not triggered onOptionsItemSelected on R.id.home

I have created the navigation drawer activity from android studio and in the method onOptionsItemSelected i'm trying to catch the click of the hamburger icon but it doesn't work.
The only thing i changed is this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == android.R.id.home) {
Log.w("ACTIVITY","home pressed");
return true;
}
return super.onOptionsItemSelected(item);
}

Back button behavior on an Activity

I have a problem in my app. I have a MainActivity with Fragments, each Fragment have a list with a recyclerView. When I click an Item of the list, the app goes to a new DetailActivity.
In the DetailActivity I have the next code line:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
When I click the back button in the ActionBar I return to the MainActivity in another fragment, not the one I clicked the item.
Thanks!
As stated by #kareem adel make sure you are not ending the previous activity. Dont use Flag_activity_clear_task or flag_activity_new_task
If that doesn't help you could just tell the back button where to go.
With getSupportActionBar().setDisplayHomeAsUpEnabled(true); set you can do this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if(id == android.R.id.home){
//Back button was hit so go somewhere.
Intent i= new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); //Clear previous Activities
startActivity(i);
finish(); //End this activity.
return true;
}
return super.onOptionsItemSelected(item);
}

Toolbar menu item not clickable on android 4.4 (19)

I have a Android app for Android sdk version 23. Now I try to make it available for user using versions 19 to 23.
All is working fine expect the toolbar in the head of the app. I can't click an menu item. Nothing happend when I click. Also if I insert Log.v() there is no message in the debug view.
What can I do?
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_refresh) {
doRefreshGames(item);
return true;
}
if(id == R.id.action_rss){
Intent rssIntent = new Intent(AmericanFootball.this, AmericanFootballRSS.class);
//if you need to pass data:
Bundle mBundle = new Bundle();
mBundle.putString("myKey", "comeon");
rssIntent.putExtras(mBundle);
startActivity(rssIntent);
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
I experienced this issue too. This was because I was using CoordinatorLayout which is a super-powered FrameLayout and thus was Overlaying the toolbar thus blocking interaction with the toolbar. I solved the issue by replacing the CoordinatorLayout with a LinearLayout and giving it a vertical orientation.
You can also solve the issue by setting the toolbar position in relation to the parent as described here

Why is `noinspection SimplifiableIfStatement` automatically added to the activity created through the wizard

My activity created through a wizard hash the following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
What is this piece of code here?
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
Without //noinspection SimplifiableIfStatement, the editor warns you because this could be simplified to:
return id == R.id.action_settings;
But that's probably not what you want here, you will need to put something in the if later (e.g. launch a Settings activity).

Dont know why my up button is not wotking

Here i setup this on oncreate method of second avticity
super.onCreate(savedInstanceState);
// Actionbar
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.new_message);
And this is onOptionsItemSelected
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_save) {
Intent newMessage = new Intent(getApplicationContext(),NewMessage.class);
startActivity(newMessage);
}
if(id == R.id.home){
Toast.makeText(getApplicationContext(), "Home button click", 2000).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
/* return super.onOptionsItemSelected(item);*/
return true;
}
Is there anything i need to change somewhere else like Manifest or some othere there is no code for back activity on MainActivity
problem:
if(id == R.id.home)
You are using the id from your R java of your project which will definitely return false it is supposed to be the native android home not your R generated id for home.
solution:
if(id == android.R.id.home)

Categories

Resources