Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shouldn't string references work for action names of an intent filter in the AndroidManifest?

Originally my AndroidManifest.xml contained an activity which I reached through its custom action name.

<activity
 android:label="HERE I AM"
 android:name="TestController">
 <intent-filter>
  <action android:name="com.company.project.TestActivity" />
  <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
</activity>

With this manifest startActivity(new Intent("com.company.project.TestActivity")); started my Activity without any problems.

But I was not satisfied with this coding style. Earlyer I was severeal times told not to use in-line defined string constants because it wolud lead to less maintainable code. And it really is a point.

So first I declared a public static final String MY_ACTION = "com.company.project.TestActivity"; in my App.java class (the one I used for ) and reached my component through this named constant this way:

startActivity(new Intent(App.MY_ACTION));

This seemed much better but I still had to maintain two instances of the same string. At http://developer.android.com/reference/android/R.styleable.html#AndroidManifestAction_name a read

... This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

So I guessed It would be fine to have this string defined JUST ONCE. I put <string name="MY_ACTION">com.company.project.TestActivity</string> in my strings.xml and modified my App:

public class App ...
    public static String MY_ACTION;

    @Override
    public void onCreate() {
        super.onCreate();
        MY_ACTION = getString(R.string.MY_ACTION);
    ...

And everything was still O.K.

... until I changed my AndroidManifest.xml to use <action android:name="@string/MY_ACTION" /> instead of <action android:name="com.company.project.TestActivity" />. After this change to my biggest surprise the application broke down at runtime complaining about not finding the Activity for my intent. The string reference was probably not parsed correctly. :(

Maybe the problem is that R.string is config dependant...? But hey I cannot be so insane to choose such a string reference whose value may change from config to config! And developer.android.com told me it's O.K. to use references at action names! I experienced this behavior at API levels 6, 7, end 8. Is it just a simple bug in the android system? Or do I misunderstand something?

I am totally puzzled, please help me. Thanks in advance.

like image 534
Zsolt Szikora Avatar asked Dec 13 '10 23:12

Zsolt Szikora


People also ask

Which of the following method can be used to set the action in an Intent object?

Action. The action in an Intent object can be set by the setAction() method and read by getAction().

What is action in intent filter?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

Can an activity have multiple intent filters?

However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another. An <intent-filter> element in the manifest file lists actions as <action> subelements. For example: <intent-filter . . . >

Which class is used for defining an intent?

Explicit intents explicitly define the component which should be called by the Android system, by using the Java class as identifier.


1 Answers

I had the same problem today, and went searching for an answer elsewhere. It appears according to Dianne Hackborne that what we are trying to do is not allowed. You apparently must use the literal strings in intent filters.

http://android.bigresource.com/Track/android-zKGKHraw9/

like image 81
MarkG Avatar answered Oct 16 '22 10:10

MarkG