转到菜单项

时间:2015-07-19 00:21:15

标签: android android-intent android-activity android-menu

我正在尝试将后退按钮添加到操作栏,以允许用户在单击菜单项后返回主活动。

我在两个菜单项活动中添加了getActionBar().setDisplayHomeAsUpEnabled(true);

在我的MainActivity.java中,我为每个菜单项创建新意图:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            // Selected settings menu item
            // launch Settings activity
            Intent intent = new Intent(MainActivity.this,
                    SettingsActivity.class);
            startActivity(intent);
            return true;

        case R.id.action_about:
            // Selected about item
            // launch about activity
            Intent i = new Intent(MainActivity.this,
                    AboutActivity.class);
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

查看logcat,当我点击操作栏中的后退图标时出现此错误:

W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client

在查找/搜索答案后,it was suggested the following change

...
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
startActivity(intent);

但它在我的情况下不起作用。我还尝试在finish()之后使用startActivity,但它不起作用,也打破了设备后退按钮

我是android的新手,如果你解释为什么我在这种情况下进行新活动后无法进入主要活动,我将不胜感激。

更新#1 :已添加MainActivity.java :(我正在关注this tutorial

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // Navigation drawer title
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private List<Category> albumsList;
    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // Getting the albums from shared preferences
        albumsList = AppController.getInstance().getPrefManger().getCategories();

        // Insert "Recently Added" in navigation drawer first position
        Category recentAlbum = new Category(null,
                getString(R.string.nav_drawer_recently_added));

        albumsList.add(0, recentAlbum);

        // Loop through albums in add them to navigation drawer adapter
        for (Category a : albumsList) {
            navDrawerItems.add(new NavDrawerItem(a.getId(), a.getTitle()));
        }

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // Setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // Enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setIcon(
                new ColorDrawable(getResources().getColor(
                        android.R.color.transparent)));

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

1 个答案:

答案 0 :(得分:0)

添加另一个案例:

 case android.R.id.home:
     onBackPressed();
     return true;

这应该有用。