Skip to content
Advertisement

How to start activity from another module

I have two modules. App and module Test. Module app contains MainActivity with button. Module Test contains MainActivity two. I want to run Test/MainActivity from module app by click on button. But Android still cannot see MainActivity from module Test. See below.

Module app Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.main">

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

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

    <activity android:name="com.example.test.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

Module Test manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Code in Module app MainActivity for click button and run second Activity:

 public void runActivity(View view) {

    Intent intent = null;
    try {
        intent = new Intent(this, Class.forName("com.example.test.MainActivity"));
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

In Console I get an error:

W/System.err: java.lang.ClassNotFoundException: com.example.test.MainActivity

EDIT: In app build.gradle file I have:

implementation project(path:':test', configuration:'default')

EDIT 2: Here is link with complete example that not working.

Advertisement

Answer

Finally, I found solutions, here: Unable to resolve dependency…

Now, Android Studio can see Module with MainActivity and I can run it.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement