Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Override method from higher API version and supporting lower API version

Tags:

java

android

I'd like to listen for a long key press in my Android application, and from Android 2.0 there is a method

public boolean onKeyLongPress(int keyCode, KeyEvent event)

to override. But what can I do if my app absolutely has to support API 4 (Android 1.6)? I know that I can call API methods with reflection, but I'm pretty sure that I cannot override with reflection.

like image 464
Axarydax Avatar asked Sep 25 '11 17:09

Axarydax


People also ask

What does @override do in Android?

@Override is an java annotation . Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

Which method does not get override?

The getFact() method is a new method declared in this FactBook class (it's not a method of the Object class), so you do not need to override it. To have a list of all the methods your class inherits, just call Ctrl+O in Android Studio.

Can you override an instance method?

3) An instance method cannot override a static method, and a static method cannot hide an instance method. For example, the following program has two compiler errors.


1 Answers

Why don't you just remove @Override annotation above the method? Android 1.6 would ignore it, 2.0 would still interpret it correctly.

like image 177
Ash Avatar answered Sep 22 '22 14:09

Ash