Tales from the jar side: Menus in Android Fragments, Chess drama, and Other tweets
I accidentally drank a bottle of invisible ink, so now I'm at the hospital, waiting to be seen (rimshot)
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of September 4 - 11, 2022. This week I didn’t have any classes, but I worked on both Android and Mockito.
Regular readers of this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader. If you wish to become a jarhead, please subscribe using this button:
As a reminder, when this message is truncated in email, click on the title at the top to open it in a browser tab formatted properly for both the web and mobile.
Menus in Fragments
The thing about Android is that it changes all the time. I taught an Android course last week, and as a demo for the students, I opened an app I hadn’t edited in a few months, just to show everybody what happens. Sure enough, Android Studio told me that several of my libraries were out of date and needed to be upgraded, and it guided me through the process.
That’s the best case scenario. Sometimes the changes are so dramatic you wish you hadn’t touched anything. But I’m usually at least aware of the new and different idioms associated with the platform, so I’m not totally surprised by them.
During our class, however, at least two relatively new features surprised me. Both came about because the book we were using (Android Programming: the Big Nerd Ranch Guide (5th edition)) emphasized fragments over activities, which I know is preferred but I rarely do.
One of them involves adding a fragment to a launcher activity. Instead of using a FragmentManager
to set the initial fragment, you can now specify the fragment you want to load in the activity’s XML layout. Previously, for example, my MainActivity
file contained the following code in the onCreate
method:
and my main_activity.xml
used a FrameLayout
that filled the window. It turns out I can delete all that code if I use a FragmentContainerView
in the XML layout and assign a name
attribute to the Fragment
:
That’s easy, which is highly unusual for Android. Normally any time they change something in Android, it makes the system more robust and powerful, but also makes everything more complicated.
The other change I ran into had to do with adding a menu to a fragment. With activities, to create a menu, you first add the menu XML file, then you override two methods:
onCreateOptionsMenu
to load the menu from the XML file, andonOptionsItemSelected
to respond to each menu item.
The Menus section in the UI guide goes into detail, but that section makes no real distinction between activities and fragments.
If you’re using a fragment, you still often let the host activity handle the menus, but if you want the fragment to add to the activity’s list of menu items, you call setHasOptionsMenu(true)
in the fragment’s onCreate
function and then you create the menu as usual.
On the other hand, if you want the fragment to handle the menu entirely, then they recommend adding a ToolBar
to your layout, then inflating the menu from the toolbar in the onViewCreated
function. The example in the documentation shows (using a “view binding” to access the toolbar):
The thing is, however, if you don’t know any better and just try overriding the normal onCreateOptionsMenu
and onOptionsItemCreated
methods as usual in a fragment, you get a deprecation warning on the superclass method call for each method that says:
Deprecated: This method is deprecated in Java
That’s highly ironic, because the onCreateOptionsMenu
function in the superclass (Fragment.java
) is a no-op, i.e., it’s an empty method. There’s nothing there. So the override calls an empty method that happens to be deprecated. (Deleting the call to the superclass method doesn’t change anything.)
So what are you supposed to do instead? The docs have a rather elaborate explanation.
This method is deprecated.
androidx.activity.ComponentActivity
now implementsMenuHost
, an interface that allows any component, including your activity itself, to add menu items by callingaddMenuProvider
without forcing all components through this single method override. As this provides a consistent, optionally Lifecycle-aware, and modular way to handle menu creation and item selection, replace usages of this method with one or more calls toaddMenuProvider
in your Activity'sonCreate
method, having each provider overrideonCreateMenu
to create their menu items.
Um, yeah. A somewhat more useful explanation comes from StackOverflow. The explanations there point you back to a section of the release notes for Version 1.4.0-alpha01 of androidx.activity, which gives that blurb above (or something similar), but with a code demo. Here’s the currently recommended way to add menus to a fragment:
A bit elaborate, but not too bad when you go through it. Get the required activity, which now implements the MenuHost
interface. Call addMenuProvider
on it, and in what Java people would call an anonymous inner class but in Kotlin is an object implementing the interface, invoke the onCreateMenu
method to inflate the menu and onMenuItemSelected
to respond to clicks.
That feels more familiar, in the sense that they took something that was reasonably simple and straightforward and made it more complicated, but more robust. That’s the Android I know.
Will they ever update the rest of the Android documentation to match this? I have no idea. But at least when the students get a deprecation warning and ask what it’s all about, I have an answer and a new way of doing what I used to do before. Plus, I have a very typical Android story to tell about making something simple complicated but “better” in some sense, which is as important a lesson for them to learn as the details of the implementation.
(I’m going to turn this into a blog post. I was planning to do so this week, but kept getting bogged down in the details.)
Magnus Did What?
It’s amazing how quickly I got tired of this whole drama, but it’s been a big news story this week and one of the rare times a chess story breaks out into the popular press.
The online magazine The Defector has a pretty good article on the situation at least as of early in the week:
Basically what happened is that Magnus Carlsen, chess World Champion since 2013, lost his third round game in the Sinquefield Cup against a 19-year-old kid named Hans Moke Niemann. Magnus then shocked everyone by withdrawing from the tournament before the fourth round started, with only this as his “explanation”:
That said nothing, and Magnus has never withdrawn from a tournament in his life, so speculation immediately ran rampant. The consensus was that Magnus accused Niemann of cheating, but wasn’t willing to say so publicly. Niemann gave a weird interview after his game which didn’t settle anything, so people started taking sides.
After his next game, Niemann gave a 30-minute extended interview vigorously defending himself, ending with:
I am not going to let Chess.com, I am not going to let Magnus Carlsen, I’m not going to let Hikaru Nakamura, the three arguably biggest entities in chess, simply slander my reputation.
He also tweeted:
The chess.com situation refers to the fact that the site (the biggest chess site on the Internet, though not one I use personally) dropped his online account, presumably in response to cheating accusations in blitz games that Niemann admitted to. He claimed they were years ago and not in money-related situations. The site basically said he was lying, but gave no additional details.
Making matters more complicated, Magnus founded his own company (called the “Play Magnus Group”) a few years ago and sold that company to chess.com about a month ago. The timing of the ban therefore raised even more suspicions.
The other reference in that quote, Hikaru Nakamura, is one of the top ten players in the world and the biggest Twitch streamer in the chess world, with over 1.5 million followers. Nakamura implied that there have been rumors about Niemann for years (although how long there can be rumors about a 19-year-old kid is not clear), but backed off later without issuing an apology.
Nobody, however, has been able to prove anything, and nobody has any idea of how you would go about cheating in over-the-board chess. Security measures were ramped up, but the tournament organizers claimed there was no evidence of anything by anybody.
Conspicuous by his silence has been Magnus himself. Garry Kasparov, a previous World Champion never shy about expressing his opinion, said:
But so far, nothing further has been forthcoming from Magnus. In the last six months, Magnus has:
Declared that he will not defend his world title.
Sold his company.
Withdrawn from a major tournament with no explanations.
Played in the World Series of Poker (and did pretty well), which is probably irrelevant but worth mentioning anyway.
Public opinion was originally backing Magnus, but has shifted to Niemann, at least until somebody provides any actual evidence of any wrongdoing. In other words, nobody knows anything, and it’s all just drama.
For the record, I’ve never cheated at chess in any platform, online or otherwise. That’s not just a moral stand — I’ve always known that if I ever cheat at anything, I’m going to get caught. I once took a left turn on a red light at an empty intersection at 3am in the morning and got pulled over by a cop immediately. That’s just my reality, so I behave accordingly.
Further updates as events warrant.
Other Random Items
One Last Rowena Follow-Up
Last week I discussed the issue with Rowena in Mr. Holland’s Opus, about why she didn’t show up at his farewell concert and if that meant anything significant.
One thing I forgot to mention was about the actress’s name. Rowena is played by Jean Louisa Kelly, who has been in a handful of feature films, as well as Broadway shows.
What I didn’t say, because it didn’t occur to me until this week, is the reason she includes her middle name. Say her name out loud without the middle name and you’ll get it right away. Go ahead. I’ll wait.
Got it? Without her middle name, she’s Jean Kelly, as in Gene Kelly.
Yeah, I can see why she might not want to go with that, though it is a nice positive association. I just can’t believe it took me this long to realize it.
The New King
Here’s the only tweet about Queen Elizabeth II’s passing that I feel comfortable sharing:
That’s funny. Okay, maybe one more:
I saw a few other funny ones. One commented on the challenges of a career where there was only one promotion available, but that you have to wait 73 years to get it. Another said something to the effect of, “the Queen is gone for one day and already Charles is made King? Nepotism is the worst.”
I think I’ll leave it at that.
Life’s Challenges
Finally, I feel compelled to include this image, from a tweet:
Indeed. We’re all doing the best we can out here.
Have a great week, everybody. :)
As a reminder, you can see all my upcoming training courses on the O’Reilly Learning Platform here and all the upcoming NFJS Virtual Workshops here.
Last week:
No classes!
This week:
Reactive Spring, an NFJS Virtual Workshop
Making New Java Features Work For You, on the O’Reilly Learning Platform
Gradle Concepts and Best Practices, an NFJS Virtual Workshop