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.
Action. The action in an Intent object can be set by the setAction() method and read by getAction().
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.
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 . . . >
Explicit intents explicitly define the component which should be called by the Android system, by using the Java class as identifier.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With