Android ActionBar wrong home button id - android

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);
}
}

Related

getSupportActionBar().setDisplayHomeAsUpEnabled(booleanValue)

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);
}

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);
}
}

click go back arrow , from same activity back to different fragment"

Iet's say I have 3 tabs, i use slidingTabLayout
so 3 tabs are 3 fragments.
i use toolbar, so each fragment has toolbar with textview and icons.
for fragment 1 and 2, icon is the same "search icon", if click it, will go to another activity for searching something.
if user at "search activity" there is a "back arrow" on tool bar, click it, user should go back to previous fragment.
if user in fragment 1, and then he click "search" he go to "search activity", and after searching, he click "back arrow", he should go back to fragment 1.
but if user in fragment 2, he click "search" he go to the same "search activity", and after searching, he click "back arrow", he should go back to fragment 2.
so i googled "click go back arrow , from same activity back to different fragment"
i did not find clue ...i think it might be something with "fragment Manager", "backPressed"....i lost myself now
I guess i might not use the correct words for google.....
I think this could work.
Have a searchable activity, a link for that is here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
In the Search activity, have something like this in the OnCreate method:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
And then also in the search activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
Hope it works!
I think your problem can be solved in onBackPressed() function.
It works like a back button.
You can try to add your arrow icon like this.
public void onClick() {
onBackPressed();
}
Hope it helps.

On home button click(in appbar/toolbar) from next activity, I need same fragment on main activity(which was there before going to next activity)

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);

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

Categories

Resources