Since upgrading to compileSDK 31 (Android 12), some users have a really strange problem.
After selecting a list item in the Recyclerview the Action Menu is not shown with my edit button. The list item is shown as selected, but the action menu is not visible (see screenshot):
If the user close or kill the app and reopen it, it works again. After some time the problem starts again.
The strange thing is that the code is executed. For example you see the statusbar is set to color gray.
My log statements from the methods:
2022-11-25 13:06:14.312 20525-20525 ActiveFragment com.xxx.xxxx I onItemClick - Position 1
2022-11-25 13:06:14.349 20525-20525 ActiveFragment com.xxx.xxxx I onCreateActionMode is called.
2022-11-25 13:06:14.350 20525-20525 ActiveFragment com.xxx.xxxx I onCreateActionMode - set status bar color.
2022-11-25 13:06:14.375 20525-20525 ActiveFragment com.xxx.xxxx I onCreateActionMode - Inflate menu_options and returning true.
2022-11-25 13:06:14.376 20525-20525 ActiveFragment com.xxx.xxxx I onPrepareActionMode is called.
2022-11-25 13:06:14.386 20525-20525 ActiveFragment com.xxx.xxxx I onPrepareActionMode - returning true.
2022-11-25 13:06:14.542 20525-20525 ActiveFragment com.xxx.xxxx I onPrepareActionMode is called.
2022-11-25 13:06:14.553 20525-20525 ActiveFragment com.xxx.xxxx I onPrepareActionMode - returning true.
2022-11-25 13:06:14.554 20525-20525 ActiveFragment com.xxx.xxxx I onItemClick - Starting the action mode and setting the title Options.
My code:
@Override
public boolean onItemClick(View view, int position) {
HyperLog.i(TAG, "onItemClick - Position " + position);
if(position == RecyclerView.NO_POSITION) {
HyperLog.e(TAG, "onItemClick - Position was NO_POSITION. Returning false.");
return false;
}
flexibleAdapter.toggleSelection(position);
// If no item is selected close the Action Mode CAB
if (checkedCount == 0) {
if(mActionMode != null) {
mActionMode.finish();
}
HyperLog.e(TAG, "onItemClick - Checked Item Count is 0, not showing ActionMode.");
return true;
}
// If the Action Mode CAB is already displayed return
if (mActionMode != null) {
HyperLog.e(TAG, "onItemClick - Action Mode is already displayed. Return true.");
return true;
}
// Start the CAB using the ActionMode.Callback defined above
mActionMode = activity.startSupportActionMode(mActionModeCallback);
if(mActionMode != null) {
mActionMode.setTitle(R.string.options);
mActionMode.invalidate();
HyperLog.i(TAG, "onItemClick - Starting the action mode and setting the title Options.");
}
return true;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
HyperLog.i(TAG, "onCreateActionMode is called.");
//hold current color of status bar
statusBarColor = activity.getWindow().getStatusBarColor();
//set your gray color
activity.getWindow().setStatusBarColor(tools.getColor(R.color.cab_color_dark));
HyperLog.i(TAG, "onCreateActionMode - set status bar color.");
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_options, menu);
HyperLog.i(TAG, "onCreateActionMode - Inflate menu_options and returning true.");
return true;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
//return to "old" color of status bar
activity.getWindow().setStatusBarColor(statusBarColor);
mActionMode = null;
selectionType = -1;
flexibleAdapter.clearSelection();
}
Update of some analyze with "LayoutInspector":
If it is not working the width and high is 0dp:
If it is working the width and high is set:
So why doesn't set the framework the width and high of the contextual action bar?
EDIT 08.12.2022:
I have now done again some debugging when this happens.
The log cat shows me this lines every time I select or deselect an item in the app (process is system.err):