start activity without knowing its class - android

Is there a way to start an Activity without knowing its class, for example via an Intent Action?
The background is, I have two Modules, ModuleMain and ModulePushNotification. ModuleMain has a dependency to ModulePushNotification. From ModulePushNotification I want to start an Activity in ModuleMain, but I cannot add a dependency ModulePushNotification -> ModuleMain because this would produce a circular dependency.
Therefore, I cannot start the activity this way:
startActivity(new Intent(this, Main.class));
I tried to start the Activity using an Intent Action:
Intent intent = new Intent("com.android.main.MY_MAIN");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
... with this Manifest in ModuleMain:
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.android.main.MY_MAIN" />
<category android:name="ANDROID.INTENT.CATEGORY.DEFAULT" />
</intent-filter>
</activity>
... but this produces the Exception:
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.main.MY_MAIN cat=[android.intent.category.DEFAULT]
Is there a way to start the main activity of the app, without restructuring the project/dependencies?

Try doing this way.
Intent intent = new Intent();
intent.setAction("com.android.main.MY_MAIN");
startActivity(intent);

Related

startActivity does not launch a new Activity of other app

I have two android apps(i.e. BIApp and EApp) and I am trying to open EApp from BIApp.
EApp has two activity one is MainActivity and another is LandingPageActivity.
When I try to launch MainActivity using startActivity(intent) it works fine but if I try to launch LandingPageActivity. startActivity(intent) does nothing.
if EApp is open in background then startActivity(intent) works for LandingPageActivity too.
Below is the code snippet that I am using.
Intent intent = new Intent();
intent.setClassName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity");
startActivity(intent);
I want to start LandingPageActivity.
What should I do ?
Try this:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
startActivity(intent);
and add
android:exported="true"
and in the intent-filter add
<category android:name="android.intent.category.DEFAULT"/>
to the declaration of LandingPageActivity in the manifest.
Try this, this is working for me
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
startActivity(intent);
Read how to allow Other Apps to Start Your Activity
You only need to add android.intent.category.DEFAULT category to your Activity onto manifest. Like below
<activity android:name="com.pkg.eapp.LandingPageActivity">
<intent-filter>
<action android:name="com.pkg.eapp.action.OPEN_LANDING_PAGE" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
and call it as
Intent intent = new Intent("com.pkg.eapp.action.OPEN_LANDING_PAGE");
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
// Or intent.setPackage("com.pkg.eapp");
startActivity(intent);
I have never done this but may be LandindPageActivity is not accessible by other apps. In your EApp manifest, do you have an intent filter like this for the LandingPageActivity ?
<intent-filter>
<action android:name="com.pkg.eapp.LandingPageActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Try this:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.pkg.eapp.LandingPageActivity");
startActivity(LaunchIntent);
Hope this might help you.

Unable to start activity through intent: ActivityNotFoundException

I have this Activity defined in my AndroidManifest.xml:
<activity
android:name=".Splash"
android:label="#string/app_name"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
When I am trying to call it in this way:
Intent splashActivity = new Intent("android.intent.action.Splash");
mainAppContext.startActivity(splashActivity);
I get:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.Splash }
And when I try to start it in this way:
Intent splashActivity = new Intent("android.intent.action.MAIN ");
mainAppContext.startActivity(splashActivity);
It open a Complete action using dialog with many options there like play store etc.
Please help in starting this Activity.
From some other Activity:
startActivity(new Intent(this, Splash.class));
Intent splashActivity = new Intent("android.intent.action.Splash");
No-no-no! It should to
Intent splashActivity = new Intent(context, Splash.class);
If you use action - it seem that you want activity for some type of action, for example, view webpage on browser.
In your situation you have to use concrete activity and set it to intent
<action android:name="android.intent.action.MAIN" />
this action is not for your app, it is needed to android's launcher, that find activity in your application to launch it
Official documentation about intent mechanism
Where are you trying to launch this activity from? The following lines imply that Splash is your main Activity:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Are you trying to launch the Splash Activity from Splash itself? This would be problematic. If you are launching from another Activity then you can use the following to launch Splash:
Intent intent = new Intent();
intent.setClass(YourCurrentActivity.this, Splash.class);
startActivity(intent);
Activity not found exception is occurred when u are trying to use or start activity that is not declared into the Mainfeast.xml file.
And When you want to start any activity then you need to start that activity by using intent. but in android "android.intent.action.Splash" Intent Action exist. And you have declared the following intent filter
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
"android.intent.action.MAIN" this is the action name that indicate the this is the activity to be started first. and android send this intent and an activity that is registered with this intent action is the first activity to be started first.
So to start splash activity you doesn't need any intent. You just need to simply run this project. but If u want to Start Another activity from splash actiivty then u need to do following things.
Intent intent = new Intent(Context, ActivityName.class);
startActivity(intent);
or
startActivityForResult(intent, requestCode);
and remember that You have to declare that activity into the mainfeast.xml

Start Activity of Main project from my library project

I have 2 projects. One is my Main Project(A) , and another is a Library Project(B). I want to start an activity which is present in A from an activity which is located in B. How do I do that ?
I Tried startActivity(getApplicationContext(),B.class);
,but
B.class
is not resolved.
How Can I let my library project start an activity of my main project ?
You shouldn't need to use an intent filter. The code in Activity A can use
ComponentName cn = new ComponentName(this, "my.package.MyActivity.B");
Intent intent = new Intent().setComponent(cn);
startActivity(this, intent);
to specify the activity B should be started.
You can add custom Action in intent-filter of you activity and start that activity by specifying action
<activity android:name="my.package.MyActivity">
<intent-filter>
<action android:name="my.package.action.MY_ACTION"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="my.package"/>
</intent-filter>
</activity>
start activity with this code:
final Intent intent = new Intent("my.package.action.MY_ACTION");
intent.addCategory(getActivity().getPackageName());
startActivity(getActivity(), intent);

Why my activity has not started up?

I am using implicit way to start an Activity. But when it runs, LogCat shows me can not find the Activity.
here is my declare of <intent-filter>:
<activity android:name=".GroupEditActivity">
<intent-filter>
<action android:name="android.intent.action.EDIT"></action>
<data mimeType="vnd.android.cursor.item/group"></data>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
Here is the code to start Activity:
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType(Groups.CONTENT_ITEM_TYPE);
startActivity(intent);
Reference said if I set the type and action the same as Intent-filter, them my Activity can start up. But when I only set action in Intent, it still can start the Activity which Intent-filter contains mimetype. So I totally lost.
here is what i find in official website:
Thanks
logcat details:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.EDITtyp=vnd.android.cursor.item/group }
And I can sure the type and action in java and xml are the same.
I solve the problem myself. The error is cause by <data mimeType. I forgot to add android here. So if change to <data android:mimeType, then everything works fine. But why it doesn't report a exception before, does mimeType is also legal?
Are you launching an activity separate from the main activity? In this case you need to call the name of that activity. If you're trying to launch your main activity, it doesn't need to call an intent.
Intent intent = new Intent(this, MyOtherActivity.class);
startActivity(intent);

Unable to launch an activity using implicit intent specifying only action

I am trying to launch an activity by specifying only its action(custom) defined in its intent filter from an activity in other application. The activity to be launched is the main activity of its application thus have android.intent.action.MAIN & android.intent.category.LAUNCHER set as action and category in its intent filter. Now according to android doc on Intent and Intent Filter, i do not need to specify DEFAULT category at all in this case. But doing the same i am i am unable to launch the activity.
LogCat says, Activity could not be found...
Am i misinterpreting the text or is there something else missing...?
Code, used for calling the activity
Intent i=new Intent();
i.setAction("android.AddressBookV4.firstActivity");
startActivity(i);
Definition of Activity being called in the manifest file
<activity android:name=".AddressBook"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.AddressBookV4.firstActivity" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You need to add the LAUNCHER category to your Intent, since the <intent-filter> says that it matches the following Intents (in pseudocode):
((action IS "android.AddressBookV4.firstActivity")
OR (action IS "android.intent.action.MAIN"))
AND (category IS "android.intent.category.LAUNCHER")

Categories

Resources