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);
}
Related
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);
}
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).
What is the listener for click event of overflow icon in action bar?
It is not detected in the onoptions selected so where else it can be detected
#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();
Log.e("id ", ""+id);
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
There is no onClick Listener for the overflow menu itself; only for items inside the menu. When you click on an element inside the overflow menu, onOptionsItemSelected() is called.
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){
...
}
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)