I have an activity with 2 fragments and drawer. So when i am in fragment i get "Up" icon instead hamburger icon (with the help of setDisplayHomeAsUpEnabled) but the action is still the same - navigation_drawer_open/close. So how to get onBackPressed() instead?
And according to this comment i dont know how to handle the Home/Up by myself because of "automatically handle clicks".
#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.
Automatic handling usually only works between activities. Since you are using fragments you may need to handle them manually.
Here's an example on how to handle a back button press.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // This is the home/back button
onBackPressed(); // Handle what to do on home/back press
break;
}
return false;
}
For do this you must use Custom Toolbar and instead of android menu key use from this library :
material-menu
In this library you can change state for arrow and rotate -90 for show Up Icon instead of Burger icon.
Related
I have a navigation drawer layout in my fragment and a floating action menu in the layout. Now when the floating action menu expands then i have to handle the click of the navigation drawer hamburger icon.
When floating action menu expands then the navigation icon should not work and don't allow drawer to open.
#SuppressWarnings("deprecation")
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
break;
}
I have tried this but, it isn't working.
If someone has a better answer that is useful please let me know.
I have a fragment on my main activity which gets loaded on click of item from nav drawer and on click of a list item from the fragment(main screen) I navigate to another activity. If I press the device back button I get my fragment screen(as expected) but if I use a tool bar and enable the the home button by saying
getSupportActionBar().setHomeButtonEnabled(true);
I get my main activity content which was prior to selecting an item from drawer. I have set parent of next activity as the main activity but I need the fragment loaded when i go back.
For my problem I found a solution. on click of the home button in the toolbar I killed my activity using finish() method and it worked just as i wanted. Hope it helps the needy.
#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();
if(id == android.R.id.home)
{
finish(); //kill subActivity so as to get same screen which was before going to subActivity
}
return super.onOptionsItemSelected(item);
}
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
You need to add this getSupportActionBar().setDisplayHomeAsUpEnabled(true);
along with getSupportActionBar().setHomeButtonEnabled(true);
In my application when the user clicks on the application logo or title, system will open the navigation drawer.
I want to make the navigation drawer to be open only if the navigation icon clicked, as i want to do some other logic if the user clicks application logo and title.
How to perform that ?
This code is intended to open navigation drawer upon clicking application logo, title or navigation icon.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
//return false;
}
}
Any advice is appreciated
I Would advice you to create a custom Toolbar using a separate XML layout and use it in your Main layout,as it is easy.
so the imageview and text View can serve for a specific purpose when clicked!
Pressing on the app's icon doesn't change the activity, but I'd like it to return to the app's MainActivity.
I've made my app's icon appear on the left of the action bar by doing the following:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
I'm assuming I should define some functionality in the onOptionsItemSelected() methods of the activities concerned. I've gotten a log of the MenuItem's ID when the app's icon is pressed and onOptionsItemSelected() gets called, but obviously I can't hardcode that id into the switch statement. What should I do?
You should set android:parentActivityName for your activity in the manifest file.
android:parentActivityName="com.example.myfirstapp.MainActivity"
You also need to handle the click in onOptionsItemSelected for the id of android.R.id.home:
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);
return true;
}
return super.onOptionsItemSelected(item);
}
See more on Android developer website.
This is how I'd do it
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.HomeButtonId){
//Return Home
}
}
Is there any way to use the standard menu button in the action bar to do something? In my case a sliding menu, I already have code that works in the onClick of a normal button. I just want to use the menu button in the action bar instead. Is this possible? Or will I have to customize the action bar and not use the button that is already there?
I believe you mean the ActionBar Home icon.
First you have to enable it
getActionBar().setHomeButtonEnabled(true);
Then you have to handle the event
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//do your work
return true;
default:
return super.onOptionsItemSelected(item);
}
}
FYI if you want to follow the Navigation drawer pattern you should read this tutorial.