Reputation: 1201
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.
Upvotes: 5
Views: 4150
Reputation: 22394
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.
Upvotes: 0
Reputation: 10938
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);
}
}
Upvotes: 3
Reputation: 2836
You can add project as external library. Also you can use maven for this kind of things.
Upvotes: 1
Reputation: 699
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!
Upvotes: 4