Unable register Activity in Manifest?
-
24-06-2021 - |
Question
Before I ask my question I saw android intent filter?
and Android: Activity not registered in the manifest
also I searched this
,but I could not find my answer.
In my App,I have to start Activty1
form IntentFilterActivity
(both are Activities) with implicit Intent
.Here is my code:
Manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.your.namespace" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="2" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".IntentFilterActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:label="Activity1" android:name="com.your.namespace.Activity1" > <intent-filter > <action android:name="com.your.namespace.MY_MAIN"/> <category android:name="com.your.namespace.MY_LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
IntentFilterActivity
package com.your.namespace; public class IntentFilterActivity extends Activity { protected String action = "com.your.namespace.MY_MAIN"; protected String category = "com.your.namespace.MY_LAUNCHER"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(IntentFilterActivity.this,Activity1.class); IntentFilterActivity.this.startActivity(i); } }); Button b2 = (Button) findViewById(R.id.button2); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.setAction(action); i.addCategory(category); IntentFilterActivity.this.startActivity(i); } }); } }
Activity1
package com.your.namespace; public class Activity1 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main1); } }
First I had one button(say b2
) in R.layout.main
and in it's onClick event I tried to start Activity1 with implicit Intent.But when I click it I get this error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.your.namespace.MY_MAIN cat=[com.your.namespace.MY_LAUNCHER] }
So I added another Buuton(say b1
) to R.layout.main
and in it's onClick event,I tried to start Activity1 with explicit Intent and Activity1 started fine.So my problem is in about registering Activity1 in manifest.
I tried to change package name,clean/build project,creating a new project and starting from there,importing the project in eclipse on a different computer,restarting eclipse and ... but I get error.My project has only one package(com.your.namespace
).
What do I do wrong?Please help me to solve this problem.
Edit:
When I change Intent filter of Activty1 to this:
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
and run App, Application's icon appears twice in Launcher
!This is strange,one intent filter seems to be ignored and another filter,not.
Solution
try this:
<activity
android:label="Activity1" android:name="com.your.namespace.Activity1" >
<intent-filter >
<action android:name="com.your.namespace.MY_MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
notice the change: android.intent.category.DEFAULT
Should work.
OTHER TIPS
Thanks Zelter Ady for his answer,I searched about startActivity()
and "android.intent.category.DEFAULT"
and found that documentation says:
In principle, therefore, an Intent object with no categories should always pass this test, regardless of what's in the filter. That's mostly true. However, with one exception, Android treats all implicit intents passed to startActivity() as if they contained at least one category: "android.intent.category.DEFAULT" (the CATEGORY_DEFAULT constant). Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters. (Filters with "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" settings are the exception. They mark activities that begin new tasks and that are represented on the launcher screen. They can include "android.intent.category.DEFAULT" in the list of categories, but don't need to.)
I hope this be helped anybody that has same problem.