从 Android-Library-Project 启动 Activity
经过长时间的搜索,我找不到任何合适的解决方案。 我有一个 Android 库项目,其中几乎包含该应用程序的所有代码。从库项目中的主要活动中,我启动了一个意图来显式活动 A。在使用库项目的派生项目中,我扩展了此活动 A 并添加了一些新代码。问题是 Activity A 的超类将响应,而不是派生类。
在使用库项目的新项目的清单中,我已使用新包声明了新活动。
这是来自库项目的 Intent 调用:
Intent i = new Intent(getApplicationContext(), AndroidActivity.class);
startActivity(i);
这是派生的 AndroidActivity 类:
public class AndroidActivity extends de.app.library.activities.AndroidActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
我无法
从库项目获取 onCreate 方法 Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.app.library"
android:versionName="1.0"
android:versionCode="1">
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".activities.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.AndroidActivity" android:label="@string/act_android" />
</application>
这里是新项目的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.app.free"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name="de.app.library.activities.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.LernenActivity" android:label="@string/act_lernen" />
所以我有:
de.app.library.activities.AndroidActivity and
de.app.free.activities.AndroidActivity
我不想对库项目做太多的更改,因为第三个项目应该使用现有的代码和未受影响的 AndroidActivity
所有其他事情对我来说都很好。例如,新项目使用更改后的布局。
我该如何处理这个问题?也许清单中我声明的新项目发生了变化,应该调用新活动而不是超类?
提前致谢!
after a long search I couldn't find any appropriate solution.
I have a Android-Library Project with nearly all code for the application. From the main activity in the library project I start an Intent to en explicit Activity A. In my derived Project, which uses the library Project, I have extended this Activity A and added some new code. The problem is that the the Superclass of Activity A will respond and not the derived class.
In the manifest of the new Project which uses the library project I have declared the new Activity with the new package.
Here is the Intent call from the Library Project:
Intent i = new Intent(getApplicationContext(), AndroidActivity.class);
startActivity(i);
Here is the derived AndroidActivity class:
public class AndroidActivity extends de.app.library.activities.AndroidActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
I am not able to get to the onCreate Method
The Manifest from the Library Project:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.app.library"
android:versionName="1.0"
android:versionCode="1">
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".activities.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.AndroidActivity" android:label="@string/act_android" />
</application>
And here the Manifest from the new Project:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.app.free"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name="de.app.library.activities.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.LernenActivity" android:label="@string/act_lernen" />
So I have:
de.app.library.activities.AndroidActivity and
de.app.free.activities.AndroidActivity
I didn't want to do that much changes to the library project beacuase a third project should use the existing code with the untouched AndroidActivity
ALl other things works fine for me. For example the changed layouts are used from the new project.
How can I handle that problem? Maybe changes in the Manifest from the new project in which I declare, that the new activity should be called instead of the superclass?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在库项目中执行此操作时,
AndroidActivity
引用库项目的AndroidActivity
:项目不了解其他外部项目,除非您将它们作为库包含在内。只需检查您的导入,您正在导入AndroidActivity
,并指示 Android 运行它。顺便说一句,很明显,如果库项目必须了解派生项目,那么这将是一个糟糕的设计模式
一个简单的解决方案是覆盖派生项目上启动的活动,如下所示:
库项目活动:
您的派生项目Activity:
因此,当您从任何地方调用 runMyActivity() 时,它将执行覆盖的函数(前提是启动活动扩展了库项目的活动并覆盖该方法)。在重写的函数上下文中,AndroidActivity.class 将是您的派生活动(或另一个,您可以导入其中任何一个,因为在这里您可以访问派生活动和库项目的活动)。
When you do this in your library project
AndroidActivity
refers to the library project'sAndroidActivity
: A project doesn't know about other external projects unless you include them as a library. Just check your imports, you are importingAndroidActivity
, and instructing Android to run it.Which by the way it's obvious, it would be a bad design pattern if the library project had to know about the derived projects
An easy solution is to override the activity launching on the derived project, something like this:
Libary project Activity:
Your derived project's Activity:
So when you call runMyActivity() from anywhere, it will execute the overriden function ( provided the startup activity extends the library project's activity and overrides that method). And in the overriden function context, AndroidActivity.class will be your derived activity (or the other one, you can import any of them because here you have access to the derived activities AND the library project's).
在您的新项目中,您可以使用不同的名称来代替 AndroidActivity。如果您不想这样做,清单文件中第二个 AndroidActivity 的完全限定名称应该可以解决您的问题。您可能在某个地方稍微搞乱了软件包。
我的意思应该是
AndroidActivity。在某种程度上,您从错误的包中选择了错误的活动类。
In your new project, you could use a different name, instead of AndroidActivity. If you don't want to do that, the second AndroidActivity's fully qualified name in the manifest file should solve your problem. You are perhaps messing up with packages a little bit, somewhere.
should be
I meant, the AndroidActivity. In some way, your are picking up the wrong activity class from the wrong package.