actionBar上的后退按钮不起作用

时间:2015-12-20 16:45:08

标签: android button android-actionbar icons

我在应用标题旁边的actionBar上有一个<按钮和一个+按钮。

enter image description here

我使用下面的代码创建<按钮

 final ActionBar actionBar = getSupportActionBar();
 actionBar.setDisplayHomeAsUpEnabled(true);
 actionBar.setHomeAsUpIndicator(R.mipmap.back_to);

然后

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {  // for + button
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.create_new_details, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.home: // back to previous activity, < button
                onBackPressed();
            break;

            case R.id.addDetails:
                mClickedPosition = -1;
                View menuItemView = findViewById(R.id.addDetails);
                PopupMenu po = new PopupMenu(this, menuItemView); //for drop-down menu
                po.getMenuInflater().inflate(R.menu.popup_details, po.getMenu());
                po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(getApplication(), "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        if ("Add Work Details".equals(item.getTitle())) {
                            Intent intent = new Intent(getApplication(), Add_Details_Information.class);  // go to Details class
                            startActivityForResult(intent, PROJECT_REQUEST_CODE);
                        } else {

                        }
                        return true;
                    }
                });
                po.show(); //showing popup menu
        }
        return super.onOptionsItemSelected(item);

    }

但只有+按钮有效。当&lt;时,我如何回到以前的活动?单击按钮?

1 个答案:

答案 0 :(得分:0)

只需将R.id.home更改为android.R.id.home