In my previous post of this series, I explained how to access resources inside an native extension for Android. In this post, I am going to explain ways to speed up access of resources. In most scenarios, you may not need it at all, but if you are using really heavy Android UI components in your extension, like the Calendar component, you are at the right place.
I had an issue where I used a calendar component in one of my native extensions. When I ran that activity natively, it took 1 second to show up, but running the same thing in native took around 9 seconds. That's huge!!!
After a lot of research, I figured out that the issue was with Context.getResourceId calls. AIR was internally running a long loop, which used to take time accessing each resource. Calendar component had 31 cells (item renderers) and each item renderer had 9 calls to Context.getResourceId. AIR doesn't maintain a dictionary of the resources once fetched, which causes it to run the loop again and again.
As a quick fix, I maintained a dictionary of resources once fetched and it brought the load time of the activity down to 3 seconds. On loading the activity again, the time was down to 1 second only.
It was still not the ideal situation. This is where a colleague of mine, a Java bond, came to rescue. The next few lines of code are sheer magic and bring the activity load time exactly the same as native, even when launched first time. Checkout the magic:
That's it done. Magic, ain't it? Just call this function to get resource id instead of using extensionContext.
Someone please pass this code on to Adobe guys, I'd give you the name of my colleague for the credits section ;)
Hope this helps and saves your life at a time where you feel completely helpless. The next one in the series, that I have posted is: Using Third party libraries (JARs) in your android code.
I had an issue where I used a calendar component in one of my native extensions. When I ran that activity natively, it took 1 second to show up, but running the same thing in native took around 9 seconds. That's huge!!!
After a lot of research, I figured out that the issue was with Context.getResourceId calls. AIR was internally running a long loop, which used to take time accessing each resource. Calendar component had 31 cells (item renderers) and each item renderer had 9 calls to Context.getResourceId. AIR doesn't maintain a dictionary of the resources once fetched, which causes it to run the loop again and again.
As a quick fix, I maintained a dictionary of resources once fetched and it brought the load time of the activity down to 3 seconds. On loading the activity again, the time was down to 1 second only.
It was still not the ideal situation. This is where a colleague of mine, a Java bond, came to rescue. The next few lines of code are sheer magic and bring the activity load time exactly the same as native, even when launched first time. Checkout the magic:
public static int getResourceId(String resourceString)
{
packageName = getActivity().getPackageName()+".R$";
String[] arr = new String[2];
arr = resourceString.split("\\.");
Class someObject = Class.forName(packageName+arr[0]);
Field someField = someObject.getField(arr[1]);
return someField.getInt(new Integer(0));
}
That's it done. Magic, ain't it? Just call this function to get resource id instead of using extensionContext.
Someone please pass this code on to Adobe guys, I'd give you the name of my colleague for the credits section ;)
Hope this helps and saves your life at a time where you feel completely helpless. The next one in the series, that I have posted is: Using Third party libraries (JARs) in your android code.
No comments:
Post a Comment