How to implement interface from newer Android API version - java

I'm working on a project that has minSdkVersion set to 7. Of course target SDK is newer, 18.
I'd like to use in my project interface Application.ActivityLifecycleCallbacks, but this is supported only from API level 14. I'm ok with not having that functionality for devices with older version of Android, still I'd like to use this interface to support newer devices with newer versions of API.
I'd be able to use Reflection or something similar if I were to only support a new class from newer API, but I don't know how to support a new interface. Can you point me somewhere? Thank you in advance.

#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void myCustomMethod(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// Your API level 14 code here
}
}

compose a wrapper that selects the calls to be made by Build SDK INT in some IF statements or use this third party lib:
https://github.com/BoD/android-activitylifecyclecallbacks-compat

Related

Using multiple sdk versions in my project

I am currently working on an app using some features that are only accessible in android 9.
I want to distinguish between users that have android 9 and can thereby use these features and users that are on 8.1 or lower for which I have to find an alternative solution.
Based on this information I want to call either the version with the features coming with android 9 or an alternative version that every smartphone can use.
My question now is, is it possible to create a project that contains android 9 "classes" but never get called when the device it is running on is 8.1 or lower.
Or is it not even possible to compile this project for a SDK version below android 9.
I am thankful for every answer.
Just check the Build version like this...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) ...

Using Java 8 with android api 22

I'm using android studio 3.0.0 Beta 5 and according to this link I should be able to use some Java 8 features for any SDK version.
But instead, I'm getting this exception for the targeted SDK 22:
default or static interface method used without --min-sdk-version >=
24
While in the previous link, it is said that Default and static interface methods features is available for any SDK.
PS: the dependency using Java 8 is Spark
The minSdk for default and static interface methods is 24. You can see the overview here
https://developer.android.com/guide/platform/j8-jack.html
But it should be available with the new Android Studio 3 according to the table here
https://developer.android.com/studio/write/java8-support.html
So it's either a documentation error or it's not ready yet.

startDrag method Deprecated and unable to compile the program

startDrag(android.content.ClipData, android.view.View.DragShadowBuilder, java.lang.Object, int) is deprecated. How to solve this without losing compatibility to the older versions? Are there any alternatives? I'm learning android basics and while trying out a simple drag and drop exercise, I encountered this error.
According to Androids API reference startDrag() was deprecated in API level 24
Use startDragAndDrop() for newer platform versions.
And since Android API level 24 equals Android N you can use:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
view.startDragAndDrop(...);
} else {
view.startDrag(...);
}
startDrag was very recently deprecated - in API 24. So you could use startDragAndDrop instead and differentiate between versions.
What you could also say is preserving compatibility to lower versions. The thing is Drag&Drop was introduced in API 11. So you could try differentiating between versions:
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
//MyDragEventListener comes in here
}
Otherwise as said, there is no official Drag&Drop for < API 11. There is not much use of implementing it for Android below API 14 (or API 15 for that matter), because per Android Studio, there are only a handful of devices running below that version, i.e. only mere 2.3%.
If you still, insist on doing it, you could use a third party library such as Android-DragArea.
Hope this helps!

About new methods and deprecated methods in Android APIs

I'm new to android development.
I'm studying with outdated books, so I'm really confused about new methods.
I'm going to make a simple and light app which can be run even on old devices.
So when I created a new android project, I set "Minimum Required SDK" to API 8, "Target SDK" to API 22 and "Compile With" to API 22.
Does this setting mean the app can work on the devices of API 8, even though I use the methods of API 22?
I'm asking this question due to deprecated methods.
I completed almost a half of my app developing using deprecated ones.
Can I just replace them all with new ones?
Or do I have to prepare multiple codes using different methods to support different platform versions?
(deprecated methods for older versions, and new ones for newer versions?)
Does this setting mean the app can work on the devices of API 8, even though I use the methods of API 22?
Yes. min sdk version is used to restrict the devices running OS with API level < minsdk from using app. Your app won't be shown in the play store for those devices.
I'm asking this question due to deprecated methods.
I haven't faced any issues till now due to depreciated methods. However, I would suggest to use min sdk version as 14 as Google has introduced many UI tweaks and enhancements. If you do some market research, using minsdk version = 14 would cover around 85% android market.
Can I just replace them all with new ones?
Yes, you can.
Or do I have to prepare multiple codes using different methods to support different platform versions?
(deprecated methods for older versions, and new ones for newer versions?)
You can do it also. Older versions do not have support have fragments. Fragments improve user experience drastically for tablets. Similarly, material design for Lollypop devices is awesome. Thee are few examples where you can opt for API level specific implementation. However, it is better to switch for api level >= 14
To clear up your confusion:
Minimum Required SDK
The minimum version of Android you're going to support. To target the largest market share, I recommend API level 14 or above.
Generally, the lower the number, the more Android devices you're targeting.
Target SDK
Basically, all this is saying is what SDK have you tested your app with. If that's KitKat then you can write in the number 19 which is the API number of KitKat, for example. You can find a list of API numbers here. As you test your app with higher and higher versions of Android, you can increase that number.
Setting the Target SDK to be a higher number will mean that you target a lot more devices.
Compile With
Specifies what API number of Android you want to compile your app with.
By default this is the latest version of Android available in the SDK Manager. Should be set high to improve user experience.
Deprecated Methods
As to your question about deprecated methods: You can continue using them. They are deprecated in favour of a newer alternative that may or may not be better in terms of functionality.
Deprecated methods will still work, the deprecated keyword just tells you that there is a newer preferred way to do it. Android is really good at backward compatibility so something compiled for API level 8 should mostly work on newer devices to.
You're going to have to use multiple code branching on version. You can check your system version at runtime like this:
private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
API 8 is really old. Have a look at the version pie chart
You can find more information about this on the Android developers website

Why I still cannot use fragment when using Eclipse(Android)?

First, I've upgraded to API-21 including eclipse android toolkit and SDK.
I want to use the fragment feature and still want to support those 2.x phones
I know that I must use support library - v4.
But the problem is when I create an Android project, it always shows "Navigation Type "Scrollable Tabs + Swipe" requires a minimum SDK version of at least 11, and the current min version is 8".
How could I solve this tricky problem?
======================================
My settings:
Minimum required SDK: API 8
Target SDK: API 17
Compile With: API 17
======================================
Thanks!!
Eclipse's project templates make use of features that require a number of APIs (mostly ActionBar), which are not provided by the support library. While you can use Fragments, with the support library, you don't have the other features associated with the APIs.
You could still implement swipe/scroll tabs by yourself though. However, you will have to create them manually (and not using Eclipse's built-in project templates).
You will need to use android.support.v4.view.PagerTabStrip, android.support.v4.view.ViewPager and android.support.v4.app.Fragment. The documentation is pretty clear and should point you in the right direction.

Categories

Resources