Android java is deprecated

Can I use deprecated methods and classes in android studio

I read Android documents and also I find deprecated methods and classes in that.Can I use deprecated methods and classes in android studio? Can I use them like other methods and classes that are not deprecated?

3 Answers 3

Yes you can use deprecated methods as long as the depreciated method exists in the framework. By deprecating a method the platform developers are trying to tell you that either something is wrong with the method or there is already better way for doing the task.

Depricating a method is like giving a warning to the developers not to use that method as the chances are high that the deprecated methods will be removed in the future release and your application which uses that method may no longer work when your users updates the platform to the latest release.

Of course you can use deprecated methods, they should still work as intended. But you have to be careful, because they could be removed in future versions. So read the comments.

The description of the Java Deprecated annotation can be found here http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Deprecated.html

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.

Источник

Android — onRequestPermissionsResult() is deprecated. Are there any alternatives?

I tried to implement request permissions for writing and reading from storage. Everything worked good but today Android showed me that the method onRequestPermissionsResult(. ) is deprecated. There are so many questions about this topic in StackOverflow, but unfortunately, they are outdated.

Читайте также:  Сброс настроек airpods через андроид

I called the methods below in a fragment.

It was suggested simply to call:

instead of my approach:

But both of them show that onRequestPermissionsResult(. ) is deprecated.

Here is my onRequestPermissionsResult(. )-method:

Here is a simple alert dialog, in which I call the onRequestPermissionsResult(. ):

Is there any alternative for onRequestPermissionsResult(. ), that I can use?

7 Answers 7

onRequestPermissionsResult() method is deprecated in androidx.fragment.app.Fragment .

So you use registerForActivityResult() method instead onRequestPermissionsResult() .

You can refer this URL.

Following is kotlin code. but you can refer it.

You can request multiple permissions.

A simple way in Kotlin

This works for me — (kotlin):

You can use some external library for permission handling to reduce some boilerplate code. I use Dexter library. Rx Permissions is also good choice if you are using RxJava2.

Most of the answers address the OP requirement. But I have found few things that are missing so I thought to provide a complete example (in Koltin)

The things that are missing are

A)Fragments must call registerForActivityResult() before they are created (i.e. initialization, onAttach(), or onCreate()). Other wise it won’t work and the app would crash.

java.lang.IllegalStateException: Fragment ProfileFragment (210ad5a1-3286-4586-a48f-deac1d8e3eef id=0x7f09008b) is attempting to registerForActivityResult after being created. Fragments must call registerForActivityResult() before they are created (i.e. initialization, onAttach(), or onCreate()).

B) It is recommended to request permission when it is really needed. In my example when user clicks on Button with id locBtn , permission dialog is shown rather than showing when activity/fragment is created.

Источник

OnActivityResult method is deprecated, what is the alternative?

I recently discovered that onActivityResult is deprecated. What should we do to handle it?

Any alternative introduced for that?

24 Answers 24

A basic training is available at developer.android.com.

Here is an example on how to convert the existing code with the new one:

The old way:

The new way (Java):

The new way (Kotlin):

EDIT. A better approach would be to make it more generalised so that we can reuse it. The snippet below is used in one of my projects but beware that it’s not well-tested and may not cover all the cases.

BetterActivityResult.java

With the above approach, you still have to register it before or during launching the activity or fragment attachment. Once defined, it can be reused within the activity or fragment. For example, if you need to start new activities in most of the activity, you can define a BaseActivity and register a new BetterActivityResult like this:

BaseActivity.java

After that, you can simply launch an activity from any child activities like this:

Since you can set the callback function along with the Intent , you can reuse it for any activities.

Similarly, you can also use other activity contracts using the other two constructors.

Источник

Display classes containing deprecation Android Studio

I updated my project to the latest Android APIs and the project now has multiple deprecated methods. Does Android Studio have a cool way of listing all classes containing said methods, such as the TODO window? I know I can go through every class and search methodically through the code, but I would rather like to make it easy on myself.

3 Answers 3

If it helps someone else heres the answer to my question:

If you go to Analyze -> Inspect Code.

When your project has been inspected click on Code maturity issues and tada, there is a list of all Deprecated API usages 🙂

UPDATE: May 2021

Deprecation warnings are now found under your respective language.

Kotlin -> Migration -> Usage of redundant or deprecated syntax or deprecated symbols

Java -> Code maturity

Follow below steps: Go to Analyze -> Run Inspectinon By Name ->type Deprecated API Usage

or in build.gradle.kts :

Then start in Terminal:

or in Gradle menu:

It will show warnings, but also can fail after 3 errors:

Caused by: org.gradle.api.GradleException: Lint found errors in the project; aborting build.

Fix the issues identified by lint, or add the following to your build script to proceed with errors:

The first 3 errors (out of 4) were:

Adding these lines in app/build.gradle won’t help. You should fix all errors and try to launch Lint again.

If you have many errors, you can show all of them:

Источник

Getter for defaultDisplay: Display!’ is deprecated. Deprecated in Java

I need the width of the screen. But recently found Android defaultDisplay deprecacted with message:

Getter for defaultDisplay: Display!’ is deprecated. Deprecated in Java

Code:

Please suggest an alternative.

8 Answers 8

defaultDisplay was marked as deprecated in API level 30 (Android R) and above. This means if you have a minimum SDK configuration below API level 30, you should have both implementations with the old deprecated code and the new recommended code.

After fixing the problem correctly you can use @Suppress(«DEPRECATION») to suppress warnings

Example: Kotlin solution

WindowManager.getDefaultDisplay() was deprecated in API level 30 in favour of Context.getDisplay() method which requires minimum API level 30.

At the moment, androidx.core.content.ContextCompat doesn’t seem to offer any backwards compatible getDisplay() method.

If you only need to retrieve the default display, instead of using different methods for different API levels as other answers suggest, you can you DisplayManager.getDisplay(Display.DEFAULT_DISPLAY) method (supported since API 17) to achieve the same result.

If what you need is to get the size of the Window, the new Jetpack WindowManager library provides a common API surface for new Window Manager features (e.g. foldable devices and Chrome OS) throughout old and new platform versions.

Jetpack WindowManager offers two ways to retrieve WindowMetrics information, as an asynchronous stream or synchronously.

Asynchronous WindowMetrics flow:

Use WindowInfoRepository#currentWindowMetrics to get notified by the library when there’s a window size change, independent of whether this change fires a configuration change.

  • Note: lifecycleScope and flowWithLifecycle() are part of Jetpack Lifecycle library.

Synchronous WindowMetrics:

Use WindowMetricsCalculator when writing code in a view where the asynchronous API can be too hard to deal with (such as onMeasure or during testing).

Источник

Оцените статью