How To Migrate The Deprecated onCreateOptionsMenu

Use the new Android addMenuProvider API instead of the deprecated Fragment menu API

Yanneck Reiß
Tech Takeaways

--

Photo by yx b from Pexels

In order to create a Menu from a child Fragment contained within a Activity, we previously had to override the respective functions directly in the respective Fragment.

However, since version 1.4.0-alpha01 of the androidx.activity library, the recommended way to set an options menu has changed.

While the Fragment menu API, which could be used for creating menu items and reacting to the selection of such, is now marked as deprecated, the ComponentActivity replaces these functionalities by implementing the MenuHost interface.

In this article, we will take a look at how we can easily manage the migration progress using this new API.

This article is also available as a YouTube video

The MenuProvider

--

--

Yanneck Reiß
Tech Takeaways

Follow me on my journey as a professional mobile and fullstack developer