Tribler / trustchain-superapp

Kotlin implementation of Trustchain and IPv8 with rich networking: multihoming of local Bluetooth+4G, decentral social networking, UDP hole punching, etc.
GNU General Public License v3.0
78 stars 63 forks source link

`setHasOptionsMenu` is deprecated #173

Open kandrio opened 1 year ago

kandrio commented 1 year ago

Overview

This is part of #161.

In a number of Fragments in the superapp, we use setHasOptionsMenu in order to declare that a given Fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu. However, the setHasOptionsMenu API is deprecated and its use produces a number of deprecation warnings when we build the superapp. Therefore, in an effort to clean the superapp from deprecation warnings, we want to replace this API with its modern counterpart.

Recommended Actions

The Android Developer documentation proposes that we use the addMenuProvider() API instead. This API function a member function of ComponentActivity. Here is the relevant code example that shows an example of how to use that API:

The Problem

The addMenuProvider() API was introduced in version 1.4.0 of AndroidX Activity. In order to declare that version, I guessed that I should add the following line to the build.gradle of each module (e.g: debug, musicdao etc...):

But that doesn't correctly upgrade ComponentActivity so that it implements the addMenuProvider() function that we need. Let's take the debug module for example to show what's going on.

  1. As you can see here, the DebugActivity inherits from the BaseActivity
  2. As you can see here, the BaseActivity inherits from the AndroidX AppCompatActivity.
  3. Through a series of inheritances (AppCompatActivity -> FragmentActivity -> ComponentActivity), we derive that DebugActivity inherits from ComponentActivity, therefore we should be able to use the desired addMenuProvider() member function of ComponentActivity.

However, when I trace back the code, I see that we're still using an outdated version of activity (version 1.2.0 instead of the desired 1.4.0) and I can't seem to update it through the build.gradle file.

@InvictusRMC Any ideas?

CC @synctext

kandrio commented 1 year ago

Update

I just found out that the latest version of the androidx.appcompat:appcompat dependency that contains the AppCompatActivity that we're using depends on androidx.activity:activity:1.1.0 which is lower than 1.4.0 where the new API was introduced. So, it seems that if we want to get rid of this deprecation warning, we must use a different Activity than AppCompatActivity.