Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 560
Rakesh Avatar asked Dec 01 '16 12:12

Rakesh


People also ask

How do I run an activity from another application?

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);

How do you communicate between modules in Android?

There are two ways to communicate between Modules in Android: Using Callbacks/ Interfaces. Using Local Broadcast.

Which method starts or calls another activity?

To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .


1 Answers

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.

like image 106
David Wasser Avatar answered Sep 22 '22 08:09

David Wasser