I am using a Project A, and a library project to develop my Android app. Both of these projects have activities in them. If I am currently in Project A, then it is easy to just start an activity in the library project by just importing it. However, I'm not sure how to start an Activity in Project A if I am coming from an activity in the library project. I'm trying to make the library project independent of the package name of the Project A since I will be using it for multiple apps. Any ideas on how to do this? Thanks.
4 Answers
There are a few possible solutions.
The best is to register an intent filter in the Manifest entries for the activities that you wish to be accessible to your other projects. There is a great tutorial on intent filters here.
Another option would be to pass a Class to the library project, and use the Intent (Context packageContext, Class cls) constructor to create an intent to fire off and start the activity. It would be a better practice and learning experience to use intent filters, however.
Good luck!

- 699
- 5
- 14
-
Would this still work if I have multiple apps installed on the same phone? Say I also have a Project B installed on the same phone, would this method be able to work for both of them? – user1132897 Aug 20 '12 at 14:53
-
You can use [createChooser](http://developer.android.com/reference/android/content/Intent.html#createChooser) to open a selection screen to pick which activity you want to handle the intent. You can find out if there is an activity to handle the intent:[Intent.resolveActivity](http://developer.android.com/reference/android/content/pm/PackageManager.html#resolveActivity%28android.content.Intent,%20int%29) – Zambotron Aug 20 '12 at 14:57
I believe this is to be the simplest answer: you'll need an object which exists in the library, which you can extend in your project.
Imagine that your library has a LibraryApplication which your ProjectApplication extends. The LibraryActivity can call:
((LibraryApplication)getApplication()).startNewActivity(this, "goHome")
Your ProjectApplication implements this new method:
public void startNewActivity(Context context, String action) {
if("goHome".equals(action)) {
startActivity(context, ProjectHomeActivity.class);
}
}

- 10,908
- 1
- 31
- 37
-
It seems that if I do this, the extended class is never called. It only calls whatever I have in the Library project. Do I need to make the method abstract or something? – user1132897 Aug 20 '12 at 17:06
-
Is this possible to call a function inside main project from library project. – vishnupriyan Mar 17 '15 at 14:09
You can add project as external library. Also you can use maven for this kind of things.

- 2,826
- 1
- 17
- 17
From a simple and good answer:
try {
startActivity(new Intent(this, Class.forName("com.package.HomeActivity")));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
And also is possible with this answer, using actions.

- 20,955
- 12
- 92
- 110