Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Write Code for Different Android Versions in Your App

4.14/5 (4 votes)
19 Jul 2013CPOL 10.7K  
Simple approach to handle fragmentation

Introduction

Fragmentation has been a big issue in Android since the beginning and everyone has different views about it. I personally don't think that there is an effective way to avoid fragmentation at the rate Android is growing at. So, fragmentation is something we should, as devs, adapt to in our developer frame of mind. 

Using the code

I know, we can always publish different apks to handle different android versions, but this may not be the optimal choice in all cases. So, here is little code snippet showing how to handle fragmentation to a certain extent in Android: 

Objective-C
@SuppressLint("NewApi")
@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        if(Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD)
        {
            //Write code specific for Gingerbread devices 
        }
        else if(Build.VERSION.SDK_INT == Build.VERSION_CODES.HONEYCOMB)
        {
            //Write code specific for HONEYCOMB devices
        }
        else if(Build.VERSION.SDK_INT == Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        {
            //Write code specific for ICE_CREAM_SANDWICH devices
        }
        else if(Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN)
        {
            //Write code specific for JELLY_BEAN devices
        }
        return true;
    }

The above code is just a simple example, that shows how you can create different menu options for different Android versions. Let me know in the comments, if you guys have any questions/suggestions. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)