getSupportActionBar().setDisplayHomeAsUpEnabled(booleanValue) - android

I was trying to implement navigation drawer into my app.
In it I came across this line of code:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
but I am not able to understand the meaning of this code.
I tried reading the official documentation but it was not clear?

These two lines used to enable and show the home button.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
You can set your own icon by adding this line: `
getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_navigation);
To perform an action on this button you will need to override onOptionItemSelected and put below code inside but make sure you write the correct id many people confuse with it.
Home button id: android.R.id.home
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home){
// write your own code..
return true;
}
}
return super.onOptionsItemSelected(item);
}

Related

Have Up button act the same as android back button

My android application has the following activity flow
Listing -> Buy -> Sell
From the listing when an item is clicked/selected an id is passed to buy to pull the relevant data from the server. If a user owns a certain item there will be a sell option as well from the buy screen (which doubles as an overview of said item).
The problem I am facing is that when you are on the sell screen and press the android back button you are taken back to the buy screen in it's original state, however when you click the back arrow (up) from the activity in the toolbar, it seems that it is actually trying to launch a new activity and throws an exception (since the activity cannot know what the id was). The buy activity is listed as a parent to the sell activity in the manifest.
I need to somehow make the up button to act the same as the back button on this particular activity, or at least pass the id back somehow.
Any suggestions would be appreciated.
If you're using an ActionBar (either platform or AppCompat) override the following method in the activity from which you wish to go back.
#Override
public boolean onOptionsItemSelected(#NonNull final MenuItem item) {
final int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
This should be safe to use in a simple navigation hierarchy such as yours.
Just yesterday i did this for my app. This is what I did:
In manifest file I removed
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
that was part of my DetailsActivity.
In the DetailsActivity I added this line to onCreate
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and in onOptionsItemSelected() method I added these lines
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
Not sure if it is the best way to handle this but oh well it works :)
In your Activity, onOptionsItemSelected() method get the clicked item id, if the id is android.R.id.home then just finish() current activity.
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Android ActionBar wrong home button id

I'm trying to implement a Navigation Drawer, I can open it by sliding my finger, however, when I click on the home button on my action bar nothing happens. I tried logging the item id in the onOptionsItemSelected() method and I confirm the id I retrieve is different from R.id.home. How do I retrieve the home button's id?
The id for home is not R.id.home, it is android.R.id.home because it is a home button created by android itself. using android.R.id.home in onOptionsItemSelected() will hopefully works for you.
Use
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
//do something
return true;
}else {
return super.onOptionsItemSelected(item);
}
}

Issue while moving from current activity to previous activity using getSupportActionBar().setDisplayHomeAsUpEnabled(true)

I have few activities and I want to move current activity to previous, like A->B->C-D, here i want to go to D->c->B->A, I have added respective parent activity name in manifest for all activities but when i press arrow in activity D its goes to activity A directly, if i backpress then it is coming to C activity,
How to to it properly?
You may override your onOptionsItemSelected-method to achieve the same behaviour in both cases...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
onBackPressed();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
This is a regular proceed of your back button. You can read more about it at the official website.
You can set flags to your intent, if you want to get other behavior.
Link

how to provide proper back navigation with fragment on clicking the home button

I would like to provide back navigation while clicking on the Home button of Actionbar.
In the Host Fragment
((MainActivity)this.getActivity()).fragment_Actualités=new Fragment_Actualités();
fragmentTransaction.replace(R.id.content_frame,((MainActivity)this.getActivity()).fragment_Actualités);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
((MainActivity)this.getActivity()).fragmentManager.executePendingTransactions();
((MainActivity)this.getActivity()).supportInvalidateOptionsMenu();
In the Host Activity
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
int id=0;
if(id==item.getItemId()){
if(id== R.id.home)
{
this.onBackPressed();
return true;}
}
The super.OnBackPressed works fine when I click on the back button it goes back as expected
How can I make it worked ?
Try using
fragmentManager.popBackStackImmediate()
Try changing:
this.onBackPressed();
to:
onBackPressed();
and extends FragmentActivity if you don't do it already
Reading your code again I think found the issue, you are assigning 0 to id so when you compere with item.getItemId() always return false. Try with this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
this.onBackPressed();
return true;
}
}
I finally resolved it,
the matter was that, I had 2 MenuItems, the Settings and the Home One's .
So if only write R.id.Home it won't work but when changed to android.R.id.Home it works !!!
with the same code

Overriding onOptionsItemSelected from SherlockFragmentActivity

Yesterday, I found a great library that allowed me to have a "facebook menu" with a button on the top left of an action bar which, when pressed, would slide in a menu of items from the left.
The problem is that I wish to make use of the ActionBarSherlock library as well to make sure that my application is backwards compatible with the action bar. When using the library I, among other things, need to override onOptionsItemSelected as such :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home) {
rbmView.toggleMenu();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
Now I went into the library and saw that the developer had made onOptionsItemSelected final. I removed it and tried overriding it again only to find that the product was that whenever I press the button nothing happens. Nothing at all.
Any idea on how I would go about using the darvds_ribbonmenu library along with actionbarsherlock?
Turns out that when using ABS you will need to specify the namespace of MenuItem to make sure that you're overriding the correct method. My solution was as follows :
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
{
int id = item.getItemId();
Log.d("item ID : ", "onOptionsItemSelected Item ID" + id);
if (id == android.R.id.home) {
rbmView.toggleMenu();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
Change import android.view.MenuItem; to import com.actionbarsherlock.view.MenuItem;. Otherwise, you're just using an entirely different MenuItem than the one you're importing.

Categories

Resources