简体   繁体   中英

Navigation drawer: unable to show the back icon instead of hamburger icon on the toolbar in the children activities

I am using the DrawerLayout with the v7 Toolbar, and I want to show the hamburger icon in the main activity, and the back icon in the children activities.

I am not able to do that, the hamburger icon is always shown, also in the children activities.

I already searched on so without success (if I lost some usefull questions I apologize).

This is the Android manifest:

<activity android:name=".activities.MainActivity"></activity>
<activity
  android:name=".activities.BuyActivity"
  android:windowSoftInputMode="adjustResize"
  android:parentActivityName=".activities.MainActivity">
  <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value=".activities.MainActivity" />
</activity>

This is the activity code (the BaseActivity is the superclass for the parent and children activities):

public class BaseActivity extends AppCompatActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_buy);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
        getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
    }

}

This is the part of the layout of the toolbar:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:background="?attr/colorPrimary">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtTitle"
        android:textColor="@android:color/white"
        android:text="@string/title_buy"
        android:textAppearance="@android:style/TextAppearance.Large" />
    <ImageView
        android:layout_width="@dimen/toolbar_image"
        android:layout_height="@dimen/toolbar_image"
        android:src="@drawable/done"
        android:background="@drawable/bg_border_white_l"
        android:layout_marginRight="@dimen/toolbar_margin_right"
        android:layout_marginEnd="@dimen/toolbar_margin_right"
        android:contentDescription="@string/app_name"
        android:layout_gravity="end"
        android:id="@+id/imgDone" />
</android.support.v7.widget.Toolbar>

I tried:

  • checked to import the correct android.support.v7.app.ActionBarDrawerToggle

I don't know what I am doing wrong.

In child activities you should not add ActionBarDrawerToggle , as this is what sets a hamburger icon. Move the ActionBarDrawerToggle to the main activity instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM