Friday, January 25, 2013

AIR Android Native Extensions: Part 7: Launching AIR's main activity from other Android activities and passing information

In my previous post, I covered, Preventing Android activity from being recreated on orientation change and relaunch.

In this post, the last of this series I would explain how to invoke your AIR application from other activities and passing data to the same. This is not about using custom URI, but about using Intent to invoke the AIR activity.

The first question that you may have is "What is AIR activity?". When you compile your Flex project into an APK, AIR automatically generates a new activity named AppEntry. This activity class is the single Android activity for all your Flex App. What I am trying to say here is that unlike Android native apps where the application is divided into multiple activities, AIR apps have just one activity i.e. AppEntry.

The package for this AppEntry class is your application's id, that you define in yourproject-app.xml. However, please note that whenever you compile your AIR app for Android, AIR prepends an "air." to the application id. So, if you have defined your app's id as "com.techie.pulkit", APK would get the id as "air.com.techie.pulkit". So, this is what the package for AppEntry class is, so the fully qualified classname for the AppEntry class, in this case, becomes "air.com.techie.pulkit.AppEntry".

Now that we know that our Air application also has an activity, how do I invoke it from my extension and pass data to it? Obviously, you could use FREContext.dispatchStatusEventAsync to dispatch events, but here we would use the Android Intent class to invoke the AIR activity and send data. Let's see how.

Activity myAIRActivity = myFREContext.getActivity();
notificationIntent = new Intent(myAIRActivity, myAIRActivity.getClass());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
notificationIntent.setData(Uri.parse("any_data_you_want_to_send"));
startActivity(notificationIntent);

The code above would allow you to invoke your AIR activity and pass the data to it. Let's go over each statement to understand better.
Activity myAIRActivity = myFREContext.getActivity();
Here, we get the reference to the AppEntry activity with the help of our extension's FREContext.
notificationIntent = new Intent(myAIRActivity, myAIRActivity.getClass());
Here, we create a new Intent using the activity's context and the activity's class.
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This one is important, read it carefully. Android Intents by default create a new instance of the activity to be started, however, we don't want that. So, we add a flag, which tells the intent not to create a new instance of the activity, but look for an existing instance and simply reorder it to front. Now, to the next step.
notificationIntent.setData(Uri.parse("any_data_you_want_to_send"))
This is where we send the data to the AIR activity. Simply parse the data as a URI and send it across. Later in the post, we'd see how to receive this data in AIR.
startActivity(notificationIntent);
Finally, simply start the activity.

Now that we are done with the Android part, let's see how to receive this data in your Flex project.

First, add a listener to the event:
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, appInvokeHandler);
Now, listen to the event like this:
private function appInvokeHandler(event:InvokeEvent):void
  {
    trace("appInvokeHandler ::: ::::: "+event.arguments.length);
  }
That's it, if the event's arguments' length > 0, you have received the data.

So, with this, I conclude this post and the series. Hope you learnt something in the process and resolved some of the issues with AIR and Android ANEs. If you'd like to go through the other posts, this is the bast place to start:
AIR Android Native Extensions - Issues and Solutions

I would try to keep sharing my findings in the field of technology as I keep getting my feet wetter. You can drop in a note to me: pulkit.gupta@gmail.com

No comments:

Post a Comment