I have an Android project. Now I need to create a second edition of that project.
What will differ:
New edition of the Project should have different content in one of the Activities.
I was thinking about creating a library project and two new projects(project1
and project2
) which will use library project, but I don't really understand how to setup this.
Should I just convert the original project into library, then create two new projects and then what? How to make project1
use activity1
and project2
use activity2
in the same place?
EDIT
What I've set up so far:
MainProject, a library has MainActivity.java and ChildActivity.java
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mainproject">
<application>
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ChildActivity">
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ChildActivity.class);
startActivity(intent);
}
});
}
}
StandAloneProject: it has only ChildActivity.java
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.standaloneproject">
<application>
<activity
android:name="com.example.mainproject.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ChildActivity" >
</activity>
</application>
</manifest>
What I expect:
When I launch StandAloneProject and click button, I want ChildActivity from StandAloneProject(not from MainProject) to be launched.
What I get:
When I launch StandAloneProject, I see MainActivity from MainProject, click button and get:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.standaloneproject/com.example.mainproject.ChildActivity}; have you declared this activity in your AndroidManifest.xml?
The problem is how you start the child activity in MainActivity.java. You need to 'manually' make sure that the correct ChildActivity is started.
One way to do this is to explicitly set the component for the intent. The Context.getPackageName() function returns the package name of the application you are running in and you can use this to identify the 'correct instance' of ChildActivity you want to start. Try this in your onClick() handler in MainActivity.java:
Intent intent = new Intent();
intent.setComponent(new ComponentName(getPackageName(), getPackageName() + ".ChildActivity"));
startActivity(intent);
Your approach is the correct one.
- Create your library project.
- Create a new project, create the activity that you need and add your library project to it.
- Copy the AndroidManifest.xml from your library project into your new project but point the activity that differs to the projects activity instead of the activity in the library project.
You are on the right track. Set up a library project and two standard android projects which include the lib project. Remember to copy your manifest activitys to the new projects. When you want to separate the activitys, you can extend the ones from your lib project. project1: MyActivty extends Activit1 .. project 2 MyActivit
来源:https://stackoverflow.com/questions/11683697/override-activity-from-android-library-project