0

I want to add a home icon on action bar in my last activity, so that the if the user is at last activity, he just click the custom home icon button on last activity and navigates to the main activity.

I created a new menu

 <item
      android:id="@+id/homeico"
      app:showAsAction="always"
      android:icon="@drawable/homeicon"
        android:title="@string/home_title"  />

and added the below code to the last activity but the icon is not there on the action bar, where i am wrong?

public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub

    MenuInflater inflate = getMenuInflater();
    inflate.inflate(R.menu.homemenu, menu);
        return super.onCreateOptionsMenu(menu);
    }
PK__
  • 187
  • 3
  • 11
  • on which menu file you have inserted above xml code? – Vilas Jan 16 '15 at 10:09
  • The back button on an Android device does exactly what you describe in this case. A back button is required on an Android device, so you there is no need to implement a 'home button' in the ActionBar. – Leon Joosse Jan 16 '15 at 10:11
  • @EagleEye i created a new xml file named home – PK__ Jan 16 '15 at 11:06
  • @LeonJoosse, a home button on action bar does not exist on main activity and only appears on secondary activities. pressing home button does not navigate out of your application. pressing back button on android device while main activity is active, will navigate out of your application. read more [here](https://developer.android.com/design/patterns/navigation.html) – AaA Aug 09 '17 at 02:20

4 Answers4

2

Do as follows in onCreateOptionsMenu(Menu menu) function of activity.

MenuItem item=menu.add("Title"); //your desired title here
item.setIcon(R.drawable.icon); //your desired icon here
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        // TODO Auto-generated method stub
            return false;
        }
    });
}
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Vilas
  • 1,695
  • 1
  • 13
  • 13
1

If you have implemented the action bar in your activity then you should be able to see the home icon by setting

getActionBar().setHomeButtonEnabled(true);

You should then override the below method to capture the "home" click event.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        /**
         * handle home button pressed
         */
        if (id == android.R.id.home) {

            //Start your main activity here

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
AncientMethods
  • 256
  • 1
  • 9
  • I wrote that but the problem is that the button is not implemented in the action bar – PK__ Jan 16 '15 at 11:10
0

Try to replace

return super.onCreateOptionsMenu(menu); 

by

return true;

EDIT: Add function to your activity

public void restoreActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}

and call it from onCreateOptionsMenu

Maxim Metelskiy
  • 1,389
  • 14
  • 29
0
  1. You need to override onOptionItemSelected() method
  2. get the Home button ID
  3. Fire an Intent to your First Activity
  4. Finish your Last Activity
kevz
  • 2,727
  • 14
  • 39
  • I know the process but the problem is that the icon that i selected is not appearing on the action bar – PK__ Jan 16 '15 at 11:11