1

I am trying to add the back button to action bar to allow user go back to the main activity after they clicked a menu items.

I added getActionBar().setDisplayHomeAsUpEnabled(true); to both menu item activities.

In my MainActivity.java I am creating new intent for each menu items:

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

looking at the logcat, when I click the back icon in action bar I get this error:

W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client

After looking/searching for an answer, it was suggested the following change:

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

but it did not work in my case. I also tried to use finish() after startActivity but it didn't work and also it broke the device back button

I am new to android and appreciate if you explain why I can't go to main activity once I am in a new activity in this case.

Update #1: Added MainActivity.java: (I am following 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);
        }
    }
Community
  • 1
  • 1
Rain Man
  • 1,163
  • 2
  • 16
  • 49

1 Answers1

0

Add another case:

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

This should work.

android_eng
  • 1,370
  • 3
  • 17
  • 40
  • also, the device back buttons work in all cases, but the back icon in the action bar doesn't work – Rain Man Jul 19 '15 at 00:52
  • try this:- case android.R.id.home: Intent homeIntent = new Intent(this, HomeActivity.class); homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(homeIntent); – android_eng Jul 19 '15 at 01:19