Hi,
I have a Drawer for which I have to set the selection in the onResume() function of the activity. The onClick event gets called correctly, the text have the correct selectedcolor, but the gray background is not there anymore, which looks not good.
It would be great to know if I am doing something wrong or if it is a real bug.
Thank you!
How do you select the item? does it have an identifier? Will it be called through the main ui thread? anything else executed afterwards which could cause this?
The item is selected with its identifier and is called through the main ui thread. The on click event triggers then a request to a server which does not interfer with the drawer.
can't reproduce this in the onPostResume method of the DrawerActivity the setSelection method will correctly highlight the item.
Please add additional details. close until then
I am seeing the same issue. I have several items in the drawer. Some are selectable (they open a new fragment) and others are not (they launch a new activity). My code figures out which item is selected. By default and on start, a selectable item is selected, which launches a new fragment. The item appears correctly highlighted. Then I click on a non-selectable item. A new activity is launched. When I return from the activity, my old item is pre-selected. However, it no longer has the highlight. Also, longclicking does not produce a ripple. However, it does produce it on the other items. What could that be?
@dragantl your item is withSelectable false? or anything like this?
No, not the one that starts a new fragment
Basic code is:
private void initializeNavItems() {
mBrowseNavItem = new PrimaryDrawerItem().withIdentifier(NAVDRAWER_BROWSE_ID).withName(R.string.navdrawer_item_browse);
mExploreNavItem = new PrimaryDrawerItem().withIdentifier(NAVDRAWER_EXPLORE_ID).withName(R.string.navdrawer_item_explore);
mCaptureNavItem = new PrimaryDrawerItem().withIdentifier(NAVDRAWER_CAPTURE_ID).withName(R.string.navdrawer_item_capture).withSelectable(false);
mSettingsNavItem = new PrimaryDrawerItem().withIdentifier(NAVDRAWER_SETTINGS_ID).withName(R.string.navdrawer_item_settings).withSelectable(false);
mLoginNavItem = new PrimaryDrawerItem().withIdentifier(NAVDRAWER_LOGIN_ID).withName(R.string.navdrawer_item_sign_in).withSelectable(false);
mLogoutNavItem = new PrimaryDrawerItem().withIdentifier(NAVDRAWER_LOGOUT_ID).withName(R.string.navdrawer_item_sign_out).withSelectable(false);
}
private void launchFragment(Fragment fragment) {
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment, TAG_FRAGMENT_CONTENT)
.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prepareNavDrawer();
}
protected void prepareNavDrawer(Bundle savedInstanceState) {
final long navItemId;
initializeNavItems();
populateNavDrawer();
if (savedInstanceState == null) {
Log.i(TAG, "Setting default ID");
navItemId = NAVDRAWER_DEFAULT_ID;
handleNavDrawerItemSelection(navItemId);
} else {
Log.i(TAG, "Fragment exists");
Fragment activeFragment = getFragmentManager().findFragmentByTag(TAG_FRAGMENT_CONTENT);
// Figure out which fragment is currently set
if (activeFragment instanceof FeedListFragment) {
navItemId = NAVDRAWER_BROWSE_ID;
} else if (activeFragment instanceof ExploreMapFragment) {
navItemId = NAVDRAWER_EXPLORE_ID;
} else {
navItemId = NAVDRAWER_DEFAULT_ID;
handleNavDrawerItemSelection(navItemId);
}
}
mNavDrawerSelection = navItemId;
}
@Override
public void onStart() {
super.onStart();
mNavDrawer.setSelection(mNavDrawerSelection, true);
}
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem == null) {
return false;
}
final long itemId = drawerItem.getIdentifier();
return handleNavDrawerItemSelection(itemId);
}
private boolean handleNavDrawerItemSelection(final long itemId) {
if (mNavDrawerSelection == itemId) {
return true;
}
if (NAVDRAWER_BROWSE_ID == itemId) {
launchFragment(FeedListFragment.create());
mNavDrawerSelection = itemId;
return false;
} else if (NAVDRAWER_EXPLORE_ID == itemId) {
launchFragment(ExploreMapFragment.create());
mNavDrawerSelection = itemId;
return false;
} else if (NAVDRAWER_CAPTURE_ID == itemId) {
startActivity(new Intent(this, CaptureActivity.class));
return true;
} else if (NAVDRAWER_SETTINGS_ID == itemId) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
} else if (NAVDRAWER_LOGIN_ID == itemId) {
// TODO: request account
return true;
} else if (NAVDRAWER_LOGOUT_ID == itemId) {
// Logging out
return true;
} else {
Log.w(TAG, "Received unknown navigation drawer id.");
}
return false;
}
@mikepenz So the issue occurs only when I select Capture | Settings -> new Activity -> return -> highlight is missing. Changing orientation doesn't fix it. Only thing that does is selecting Explore (which launches a fragment... could be Explore -> missing highligh -> Browse). Also, initial load properly selects and highlights. It is only after the return from a started Activity.
@dragantl interesting. will require some debugging :/
@mikepenz for additional information, I did examine view hierarchy. In both cases, with and without highlight, the view reports as being selected:

@dragantl on which device are you testing this?
It is really weird as the "selected" state should be probably handled by the system.
https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/model/BaseDescribeableDrawerItem.java#L81
Here I define a StateListDrawable
https://github.com/mikepenz/Materialize/blob/develop/library/src/main/java/com/mikepenz/materialize/util/UIUtils.java#L259
@dragantl @vincepunkrock are you using the latest versin of MaterialDrawer & FastAdapter ?
I have something really strange. I was somehow able to reproduce this earlier today. But I am now no longer be able to do so. It will now work all the time, which is weird.
Are you also on Android N?
I use the version 5.6.0 of Material Drawer, which is the latest as far as I
know.
I am also on Android N yes.
On Fri, Sep 16, 2016 at 5:01 PM, Mike Penz [email protected] wrote:
I have something really strange. I was somehow able to reproduce this
earlier today. But I am now no longer be able to do so. It will now work
all the time, which is weird.Are you also on Android N?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mikepenz/MaterialDrawer/issues/1514#issuecomment-247623690,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJLMLE5EhjoMzZ_KGqnbZYUMMVCwjAMyks5qqq9lgaJpZM4J1re9
.
@vincepunkrock weird. so prbalby it is somehow related to this, that the system is stopping what it should do in rare cases :O
Very weird indeed!
On Fri, Sep 16, 2016 at 5:10 PM, Mike Penz [email protected] wrote:
@vincepunkrock https://github.com/vincepunkrock weird. so prbalby it is
somehow related to this, that the system is stopping what it should do in
rare cases :O—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mikepenz/MaterialDrawer/issues/1514#issuecomment-247626073,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJLMLLx0leuwGeer29s_xLDXQWWdViTdks5qqrF6gaJpZM4J1re9
.
@mikepenz I am also using 5.6.0 with device running M but app is compiled with min SDK 17 and target SDK 21. Activity in which the drawer is located is using theme Theme.AppCompat.Light.NoActionBar. Its pretty consistent for me.
@dragantl what happens if you reselect this item in onResume will it switch the state properly
@dragantl I can only believe that it has to be framework or appcompat related as I depend on the framework for showing the drawable's selcted state.
https://github.com/mikepenz/FastAdapter/blob/develop/library/src/main/java/com/mikepenz/fastadapter/FastAdapter.java#L924
This will dispatch the sstate updates to it's view, and drawable. And as posted above I use a StateListDrawable as background:
https://github.com/mikepenz/Materialize/blob/develop/library/src/main/java/com/mikepenz/materialize/util/UIUtils.java#L259
Which has the selected state:
https://github.com/mikepenz/Materialize/blob/develop/library/src/main/java/com/mikepenz/materialize/util/UIUtils.java#L262
EDIT:
I just remember something from a long time ago. There was an issue that the animation is causing troubles:
https://github.com/mikepenz/FastAdapter/issues/163#issuecomment-232742338
I have the animation enabled here: https://github.com/mikepenz/Materialize/blob/develop/library/src/main/java/com/mikepenz/materialize/util/UIUtils.java#L267
So perhaps this is causing some troubles on specific android versions, target versions, phones, ...?
Close as no new infos were addd.
I am now having this issue too where it doesn't give the grey background only changes the text color
Most helpful comment
I am now having this issue too where it doesn't give the grey background only changes the text color