3

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 ?

Community
  • 1
  • 1
harveyslash
  • 5,906
  • 12
  • 58
  • 111
  • Have you added your Activity B in the manifest of your Project A? – sergiomse Sep 01 '14 at 11:49
  • Have u Added Lib Project in Main Project – Naveen Tamrakar Sep 01 '14 at 11:49
  • There are ways to do what you want, however that approach is not the right one. It's a cyclic reference basically, which is something you want to avoid. Declare some listener interface in the library and set a listener from the main project instead. – nitzanj Sep 01 '14 at 11:49
  • i have added lib project in main – harveyslash Sep 01 '14 at 11:49
  • Just read these articles [Allowing Other Apps to Start Your Activity](http://developer.android.com/training/basics/intents/filters.html) and [Starting Another Activity](http://developer.android.com/training/basics/firstapp/starting-activity.html). Hope this will resolve your problem. – jitain sharma Sep 01 '14 at 11:50
  • no, this is not something I was looking for. You are telling me that there is no way an activity can be started without an intent filter? – harveyslash Sep 01 '14 at 11:52
  • this might help you http://stackoverflow.com/questions/2209513/how-to-start-activity-in-another-application – Michael Shrestha Sep 01 '14 at 11:59
  • What's wrong with intent filter? For me it's a most elegent way to solve the problem. – Dmitry Ryadnenko Sep 01 '14 at 12:07

2 Answers2

6

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.

zmarties
  • 4,809
  • 22
  • 39
2

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);
Dmitry Ryadnenko
  • 22,222
  • 4
  • 42
  • 56