Problem calling activity from another package in Android

房东的猫 提交于 2020-01-06 17:14:42

问题


Situation: I need to combine several apps into one .apk app. Lets say implement app2 into app1

What i have done: Copied app2 package into main app1 project which i am working, so my app1 has two packages.

app2 had this manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.app2" android:versionCode="3" android:versionName="1.2">

<application android:label="App2" android:icon="@drawable/icon">
    <activity android:name="Activity1" android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>        

<activity android:name="Activity2" android:excludeFromRecents="true"></activity>
</application>

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
</manifest>

My original App1 manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.package.app1">
<application android:icon="@drawable/icon" android:debuggable="true">
    <activity android:name=".Start" 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="com.package.app1.PlayerList" />
    <activity android:name="com.package.app1.CreateNewPlayer" />
    <activity android:name="com.package.app1.Profile" />                       
    <activity android:name="com.package.app1.Braintrainer" />
</application>
</manifest> 

The code in app1 i am using to call activity in app2 package:

Intent i = new Intent();
i.setClassName("com.package.app1", "com.package.app2.Activity1");                    
startActivity(i);

The question: How do i modify my app1 manifest file to have activities of app2.

Things i`ve tried: It works if i create simple HelloWorld test class in app2, call using the same code and just include this in the app1 manifest:

        <activity android:name="com.package.app2.Test" />

But i can not figure out how to implement the app2 manifest file into the first one. Every way i try give no errors but crashes when calling that activity. App2 alone works fine, so problem not in the activity file.

Appreciate any thoughts on this.


回答1:


Intent i = new Intent();
i.setClassName("com.package.app1", "com.package.app2.Activity1");                    
startActivity(i);

Shot in the dark:

change com.package.app1 to com.package.app2. I've called done what you're attempting right now, and I've always had to specify the package of the class I wanted to call.

Ok, PackageManager is not your solution, I misread and thought you had two apps and wanted one app to call the other. It looks like you just want one app.

Modify your app1's manifest like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.package.app1">
<application android:icon="@drawable/icon" android:debuggable="true">
    <activity android:name=".Start" 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="com.package.app2.Activity1" />
    <activity android:name="com.package.app2.Activity2" android:excludeFromRecents="true"></activity>
    <activity android:name="com.package.app1.PlayerList" />
    <activity android:name="com.package.app1.CreateNewPlayer" />
    <activity android:name="com.package.app1.Profile" />                       
    <activity android:name="com.package.app1.Braintrainer" />
</application>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
</manifest> 

And try the first way again.




回答2:


I have several packages and need to do something similar in my app.

You need to be careful with this technique, otherwise you'll experience nasty memory leaks.

I declared my activity in my base controller (activity) class as a static variable. All controller classes inherit from this class, so all controller classes are able to access it. Pass in the activity as an argument for anything outside of the controller classes that need to access the activity.



来源:https://stackoverflow.com/questions/6874218/problem-calling-activity-from-another-package-in-android

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