-1

I have an action bar up button and overflow menu in my android application. The overflow menu is working fine but the action up button is not working at all. There is no error at all as the log is not showing anything.

Below is my Code for Overflow Menu button:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:checkableBehavior="none">
        <item
            android:id="@+id/emailPdf"
            android:title="Email" />
        <item
            android:id="@+id/printPdf"
            android:title="Print" />

    </group>
</menu>

And, its java code is:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_action_bar_menu_options, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        super.onOptionsItemSelected(item);

        switch(item.getItemId()){
            case R.id.email:
                Toast.makeText(getBaseContext(), "You selected Email", Toast.LENGTH_SHORT).show();
                break;

            case R.id.print:
                Toast.makeText(getBaseContext(), "You selected Print", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;

    }

Also, code for the action up button:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

Code in Android Manifest File:

   <activity
            android:name=".ChildActivity"
            android:label="@string/title_activity_child"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.sample.sample.MainActivity" />
      </activity>

When I remove the java code for Overflow Menu, then only the Action Up Button works. I have no idea why is it happening. Help would b appreciated.

Thanks in advance.

3
  • you have to add if (item.getItemId() == android.R.id.home) { onBackPressed(); return true; } inside onOptionsItemSelected() May 2, 2017 at 10:09
  • @DivyeshPatel Thank You so much mate. It worked. Thanks buddy. May 2, 2017 at 10:15
  • If answer help you, Upvote my answer. May 2, 2017 at 10:17

2 Answers 2

0

Change onOptionsItemSelected code to this.

   @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                if (item.getItemId() == android.R.id.home) {
                    onBackPressed();
                    return true;
                }else if(item.getItemId() == R.id.email){
                        Toast.makeText(getBaseContext(), "You selected Email", Toast.LENGTH_SHORT).show();

                }else if(item.getItemId() == R.id.print){
                         Toast.makeText(getBaseContext(), "You selected Print", Toast.LENGTH_SHORT).show();

                }
                return super.onOptionsItemSelected(item);
            }
0

@Override public boolean onOptionsItemSelected(MenuItem item) {

super.onOptionsItemSelected(item);

switch(item.getItemId()){
    case R.id.email:
        Toast.makeText(getBaseContext(), "You selected Email", Toast.LENGTH_SHORT).show();
        break;

    case R.id.print:
        Toast.makeText(getBaseContext(), "You selected Print", Toast.LENGTH_SHORT).show();
        break;
    case R.id.home:
      // Simulate back button press.
         onBackPressed();
         break;
}
return true;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.