Question

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.

Was it helpful?

Solution

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top