Start Activity of Main project from my library project - android

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);

Related

start activity without knowing its class

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);

start an application from another

I'm developing two applications, AppA and AppB, and I want to start AppB from AppA.
In AppA, I am using
Intent initIntent = getPackageManager().getLaunchIntentForPackage("com.example.appB.ActivityB");
in AppB, I am adding an intent filter to the manifest file :
<activity
android:name="com.example.appB.ActivityB"
android:label="#string/title_activity_init" >
<intent-filter>
<action android:name="com.example.appB.ActivityB" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
But I got a java.lang.NullPointerException on the intent...
Any ideas would be welcome.
Your intent filter isn't complete for this API:
The current implementation looks first for a main activity in the category CATEGORY_INFO, and next for a main activity in the category CATEGORY_LAUNCHER. Returns null if neither are found.
So you need to modify Activity B's intent filter to include:
<action android:name="android.intent.action.MAIN" />
You are trying to specify an Activity in the Intent, even though you are creating a Intent meant to launch a package; this is not neccesary. Simply remove the Activity from your Intent like so:
Intent initIntent = getPackageManager().getLaunchIntentForPackage("com.example.appB");
And then just make sure that you have the following two lines in whichever Activity you wish to start when the application is launched:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
They should be already be in there for your main Activity, although it looks like you were missing the android.intent.action.MAIN intent filter in your ActivityB.

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

Unable to find explicit activity class in android

I have an android project with several packages. The structure of the packages in this case is com.siva.restorative is the package that contains the activity I want to run.
My activities are declared in my manifest as
<activity android:theme="#style/YtdTheme" android:name="com.siva.restorativecare.RestorativeCare">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The mainScreen activity displays fine, since it is inside the com.WAPP package. But when I try to run the setLocationActivity, I get the unable to find explicit class error. Here is how I have the intent parameters:
Intent i = new Intent(this,SecondActivity.class);
I guess first you should change the action string in manifest as:
<action android:name="com.siva.restorativecare.RestorativeCare" />
and then start running your activity as:
Intent i = new Intent("com.siva.restorativecare.RestorativeCare");
startActivity(i);

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);

Categories

Resources