Parameterized Activity in android

Android activities can be invoked by other applications installed on a device by use of Intents. That’s the way the platform was designed, to use the benefit of fact that other apps are able to do something and we don’t need to implement that functionality. You would probably think: “That’s great!” and yes it is but there’s always a catch.

Let’s take a look at rare scenario when you user use Honeycomb tablet (I know… old… not visible on a market… this is only an example). You now that in activity called TestActivity you have code that won’t be able to run on this version of android. You can write some if statements in you java code to handle that kind of situation. OK but there’s more elegant solution. Why even show this activity to other applications? We can accomplish that by using parameters in AndroidManifest.xml.

In android we can define resources that are specific to API version that user device is using. That kind of resources are usually strings or selectors but we can also define there booleans. For example let’s create a folder values-v11 and inside of it file bools.xml with content like below.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- True if running under Honeycomb or later. -->
    <bool name="atLeastHoneycomb">true</bool>
</resources>

Then we would create next file for API other versions. Now let’s add checking this parameter in our application tag in manifest.

<activity android:name="com.example.project.TestActivity"
                  android:enabled="@bool/atLeastHoneycomb" >
            . . .
</activity>

Now our activity won’t be visible to other apps installed on device running Honeycomb. Attributes defined this way can be used in many other situations.

Leave a comment