从Android电子图书馆项目启动活动 [英] Starting Activity from Android-Library-Project

查看:120
本文介绍了从Android电子图书馆项目启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个漫长的探索后,我找不到任何合适的解决方案。 我有一个Android的库项目与几乎所有的code应用程序。从库项目的主要活动我开始的意图,EN明确活动答:在我的派生项目,该项目使用库项目,我已经延长这一活动的,并增加了一些新的code。问题是,活动A的超类将响应,而不是派生类。

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);

下面是派生AndroidActivity类:

Here is the derived AndroidActivity class:

public class AndroidActivity extends de.app.library.activities.AndroidActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
            ...

我不能够去的onCreate方法

I am not able to get to the onCreate Method

从图书馆计划的清单:

<?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

我不想做太大的变化到库项目beacuase第三个项目应利用现有的code与不变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?

在此先感谢!

推荐答案

当你这样做你的库项目

 Intent i = new Intent(getApplicationContext(), AndroidActivity.class);

AndroidActivity 指的是库项目的 AndroidActivity :项目不知道其他外部项目,除非你有它们作为一个库。只要检查你的导入,要导入 AndroidActivity ,并指示机器人运行它。

AndroidActivity refers to the library project's AndroidActivity: A project doesn't know about other external projects unless you include them as a library. Just check your imports, you are importing AndroidActivity, 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项目活动:

Libary project Activity:

public void runMyActivity() {
    Intent i = new Intent(getApplicationContext(), AndroidActivity.class);
    // You are on the library project, so you can refer to the activities created in it.
    // the only AndroidActivity known here is the library project's
    .
    .
}

您导出项目的活动:

@Override
public void runMyActivity() {
    // You are now on the derived project, so you can refer to 
    // the activities created in it, and also to the library project's. You
    // just import the package name of the desired activity or use fully qualified
    // names

    Intent i1 = new Intent(getApplicationContext(), AndroidActivity.class);
    // this one will use the activity in the imports
    Intent i2 = new Intent(getApplicationContext(), de.app.libarary.AndroidActivity.class);
    // use the activity from the library project
    Intent i3 = new Intent(getApplicationContext(), de.app.free.AndroidActivity.class);
    // use the activity from the derived project

    .
    .
}

所以,当你从任何地方拨打runMyActivity(),它将执行被覆盖的功能(提供的启动活动来延长库项目的活动,将覆盖方法)。而在被覆盖的功能范围内,AndroidActivity.class将是派生的活动(或另外一个,你可以因为在这里你可以访问派生活动,并导入其中的任何库项目的)。

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).

这篇关于从Android电子图书馆项目启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆