Override Activity from Android Library project

百般思念 提交于 2019-12-04 12:16:32

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.

  1. Create your library project.
  2. Create a new project, create the activity that you need and add your library project to it.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!