Rakesh
Rakesh

Reputation: 1236

How to invoke an Activity (belonging to a module) in another module in Android?

Here's the scenario : I have 2 modules (In Android Studio, File -> New -> New Module) in my single application.

  1. Module A
  2. Module B

Module A (Its not a library project. it's gradle starts with apply plugin: 'com.android.application').

Module B (which is also not a library module).

Inside module B, I need to invoke an Activity (say MainActivity) which belongs to module A.

Module A manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.emergencycontacts">
    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true">
<activity android:name=".EmergencyContactsActivity" android:theme="@style/AppTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

Module B manifest :

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

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true">

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

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

</manifest>

How do I achieve it?

Please note that I cannot add module A's dependency in module B, since module A is not a library module.

Awaiting your valuable response.

Upvotes: 12

Views: 13735

Answers (1)

David Wasser
David Wasser

Reputation: 95568

To launch any Activity from any application, you can just do this:

Intent intent = new Intent();
intent.setClassName("packageName", "className");
startActivity(intent);

You don't need to be able to reference the source code of that Activity during compile time.

This will solve your stated problem.

Upvotes: 2

Related Questions