AF1001 AF1001 - 1 year ago 79
Android Question

Home button changing from drawer icon to up button on rotation - only when nav drawer is open

I have implemented a navigation drawer in my app and for the most part it appears to be working OK.

I have the home button set to be the nav drawer icon(hamburger) and this persists across normal rotations. As can be seen in Image 1 (portrait) and Image 2 (landscape after rotation).
Image 1

Image 2

However If i open the nav drawer, then rotate the screen and then close the nav drawer, the home button changes to the up button, as can be seen in image 3.
Image 3

I have tried changes things around so that it constantly remains the hamburger icon but have had no luck.

My Code for the MainActivity is below.

public class MainActivity extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
private String mDrawerTitle;
private String mTitle;
ActionBarDrawerToggle mDrawerToggle;
private static int sNavPosition = 0;
private static final String NAV_POSITION_KEY = "navKey";
private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerTitle = getTitle().toString();
mTitle = getTitle().toString();
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.drawer_fragment_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
setUpToolbar();
NavDrawerItem[] navDrawerItems = new NavDrawerItem[4];

navDrawerItems[0] = new NavDrawerItem(getString(R.string.nav_home));
navDrawerItems[1] = new NavDrawerItem(getString(R.string.nav_aims));
navDrawerItems[2] = new NavDrawerItem(getString(R.string.nav_arttrail));
navDrawerItems[3] = new NavDrawerItem(getString(R.string.nav_submission));

DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.drawer_list_item,navDrawerItems);
View header = getLayoutInflater().inflate(R.layout.drawer_header,null);
mDrawerList.addHeaderView(header);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.addDrawerListener(mDrawerToggle);
setUpDrawerToggle();
if (savedInstanceState != null && savedInstanceState.containsKey(NAV_POSITION_KEY)){
sNavPosition = savedInstanceState.getInt(NAV_POSITION_KEY);
}
new DrawerItemClickListener().selectItem(sNavPosition);
}

private void setUpDrawerToggle(){
mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.app_name,R.string.app_name);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
}



@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}

private void setUpToolbar(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

}



@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putInt(NAV_POSITION_KEY,sNavPosition);
}


}

I have excluded unnecessary code regarding click listeners for navigation.

Any help or suggestions would be greatly appreciated. Thank you.

Answer Source

Try Removing getSupportActionBar().setDisplayHomeAsUpEnabled(true);and
mDrawerToggle.setDrawerIndicatorEnabled(true); from ur
setUpDrawerToggle();method

Edit: setDisplayHomeAsUpEnabled(false) hides the default back button
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download