Patrick Simons
Patrick Simons

Reputation: 21

Android start activity in different java package

I'm building an Android application that needs to have customer specific code. Customer specific code needs to be separated from the actual Android product our company supplies.

To do this I tried to create 2 packages:

Both packages contain ExampleActivity. I have written a factory that uses reflection to determine of a custom component exists on top of the product class. This works fine.

Using the following code to start the ExampleActivity of product works:

    Intent intent = new Intent(this, com.company.product.activity.ExampleActivity.class);
    startActivity(intent);

Using the following code to start the ExampleActivity of customcode fails:

    Intent intent = new Intent(this, com.company.product.customcode.activity.ExampleActivity.class);
    startActivity(intent);

Error:

    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.product.customername/com.company.product.customcode.activity.ExampleActivity}: java.lang.IllegalArgumentException: AppIndex: The URI host must match the package name and follow the format (android-app://<package_name>/<scheme>/[host_path]). Provided URI: android-app://com.company.product.customcode.activity/http/host/path
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)

I also tried this code, but then Android gives a Toast that it cannot find the Activity:

    Intent intent = new Intent();
    intent.setClassName("com.company.product.customcode.activity", "com.company.product.customcode.activity.ExampleActivity");
    startActivity(intent);

Manifest:

    <activity android:name="com.company.product.activity.ExampleActivity"
                android:label="@string/app_name"
                android:noHistory="false"
                />

    //Custom implementation of the ExampleActivity
    <activity   android:name="com.company.product.customcode.activity.ExampleActivity"
                android:label="@string/app_name"
                android:noHistory="false"
                />

Does anybody have any ideas or tips how to achieve the maingoal: To split custom code from the productcode where activity names might be equal.

Upvotes: 2

Views: 1734

Answers (4)

Patrick Simons
Patrick Simons

Reputation: 21

I've solved this question. It seemed by further investigation that the Activity crashed because of some auto-generated code that was added while copy-pasting.... It seemed like a crash before the Activity was loaded, but this apparently was not the case.

Thank you all for you're answers, they were very useful!

Upvotes: 0

sam08
sam08

Reputation: 53

There is nothing wrong in the code.I had tried the same,it seems no issue.

Upvotes: 1

Raghavendra
Raghavendra

Reputation: 2303

I am not sure what you missed here. I have tried this and it worked.

I have created a class MainActivity under package com.sample.so_sample.activities

and another MainActivity under package com.sample.so_sample1.test.activities

My manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.so_sample">

 <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".activities.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.sample.so_sample1.test.activities.MainActivity">
        </activity>
 </application>
</manifest>

and my call to navigate in com.sample.so_sample.activities.MainActivity is

Intent i = new Intent(this, com.sample.so_sample1.test.activities.MainActivity.class);
startActivity(i);

Upvotes: 1

Oleg Vladimirov
Oleg Vladimirov

Reputation: 157

Differen package names is the right way, it has to work fine, seem like problem is out of this code.

Your code can be in any java packages, you need just specify fully qulified name of activity in manifest. Android package name it's just a unique app id string. instead of:

Intent intent = new Intent();
intent.setClassName("com.company.product.customcode.activity", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);

try:

Intent intent = new Intent();
intent.setClassName("YOUR PACKAGE NAME IN MANIFEST", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);

Upvotes: 0

Related Questions