简体   繁体   English

Android:从主应用程序启动库活动

[英]Android: Launching Library Activity from Main App

i already readed the "similar questions" but i'm still not able to launch the Library Project Activity from the Main Project. 我已经阅读了“类似的问题”,但仍然无法从主项目启动“图书馆项目活动”。

I have project A (Main) and i have project B (library) 我有项目A(主要),我有项目B(图书馆)

Activity is declared in the library manifest (even in main manifest) 活动在库清单中声明(甚至在主清单中)

but when i run the code the log throws the error that ask if activity is declared in manifest.xml file. 但是当我运行代码时,日志抛出错误,询问是否在manifest.xml文件中声明了活动。

Main Project is rightly including the library, everything compiles but the error is that, i put the intent i'm trying from main project: 主项目正确地包括库,一切都可以编译,但是错误是,我打算从主项目中尝试:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.package.activities", "MyActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage(context.getPackageName()); // tried without this
context.startActivity(intent);

this is my manifest (from library project): 这是我的清单(来自图书馆项目):

<activity
        android:name="com.package.activities.MyActivity"
        android:exported="true"
        android:label="@string/title_activity"
        android:screenOrientation="landscape"
        android:configChanges="keyboardHidden|orientation"/>

Log: 日志:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.package.activities/MyActivity}; android.content.ActivityNotFoundException:无法找到显式的活动类{com.package.activities / MyActivity}; have you declared this activity in your AndroidManifest.xml? 您是否在AndroidManifest.xml中声明了此活动?

Give this a try: 试试看:

Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

When you try to launch the activity as a separate component, Android starts looking for this library activity within your main project code. 当您尝试将活动作为单独的组件启动时,Android开始在您的主项目代码中查找此库活动。 As the activity belongs to the library and not the main project, you get the ActivityNotFoundException . 由于活动属于库而不是主项目,因此将获得ActivityNotFoundException

Given that you have added the library project as an AAR file to the main project, you can simply launch the library activity from the main project as follows: 假定您已将库项目作为AAR文件添加到主项目中,则可以简单地从主项目启动库活动,如下所示:

Intent intent=new Intent(this, "your.library.package.activityname.class")
startActivity(intent)

You must declare the library activity in the manifest file of your main project. 您必须在主项目的清单文件中声明库活动。 A single line should suffice 一行就足够了

<application ...>
    <activity android:name="your.library.package.activityname" />
</application>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM