Sunday, 8 January 2012

Android - Advanced topics I - Legacy Activity vs NewActivity

From the video of Google I/O 2011: Android Protips: Advanced Topics for Expert Android App Developers, I am going to extract a set of codes that could be useful. This first post is about the kind of activity to start depending on the version of Android. (Minutes 06:07 of the video)


private static boolean shinyNewAPIS = 
   android.os.Build.VERSION.SDK_INT >= 
      android.os.Build.VERSION_CODES.HONEYCOMB;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   Intent startActivityIntent = null;
   if (!shinyNewAPIS) {
      startActivityIntent = new Intent(this, LegacyActivity.class);
   } else {
      startActivityIntent = new Intent(this, HcActivity.class);
   }

   startActivity(startActivityIntent);
   finish();

}

From the current statistic information, as shown below, most of Android version starts from Froyo (Android 2.2, API 8). 

So, instead of using HoneyComb (Android 3.0, 3.1 and 3.2, API 11, 12 and 13), my recommendation is utilize the Froyo. See the code below (With API 7). 

private static boolean shinyNewAPIS
  android.os.Build.VERSION.SDK_INT
      android.os.Build.VERSION_CODES.ECLAIR_MR1;


Final version of code at GitHub:
https://github.com/jiahaoliuliu/com.jiahaoliuliu.android.VersionDependency/tree/master

No comments:

Post a Comment