0

How can I launch an Activity from another Application Module within the same project?

I am not talking about how to open an Activity from another application, therefore this is not a duplicate of Launch an application from another application on Android


I have 2 modules: mobile and tv. Both are com.android.application on their build.gradle files and both are on the same package com.example.myapp.

I want the tv module to launch the MainActivity of the mobile module.

I've added the following line to my build.gradle(tv) file:

compile project(path: ':mobile')

on MainTvActivity.java I tried to reference the MainActivity

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

which didn't work, because it couldn't resolve the symbol MainActivity. It won't even build.


The other attempt was to use intent filters:

on AndroidManifest.xml(mobile)

    <activity
        android:name="com.example.myapp.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.example.myapp.mobile" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

on MainTvActivity.java:

Intent intent = new Intent("com.example.myapp.mobile");
startActivity(intent);

which will build and install but will crash because it won't find anything to resolve the intent. However, if I install the mobile version and then run the tv one, it will work (as expected).

enter image description here

Community
  • 1
  • 1
Rauter
  • 511
  • 2
  • 5
  • 14
  • how do you make the apps talk to one another? are they connected? – Hala.M Oct 18 '16 at 12:56
  • They are two modules within the same project, but of the application type. They don't have to talk to each other, the tv app only has to start an activity of the mobile app – Rauter Oct 18 '16 at 13:43
  • I didn't do this before but it sounds as two different modules in two different devices which isn't connected maybe if you have a server you can use it to send a broadcast receiver to launch the activity – Hala.M Oct 18 '16 at 13:52
  • No no no! They are modules inside a project, or so it is how Android Studio would call them. I added a screenshot to the question, maybe it will be clearer. – Rauter Oct 18 '16 at 14:10
  • I got that I meant they are on different devices – Hala.M Oct 18 '16 at 14:14
  • No, they are only in one device. I want to be able to install only the tv app, but launch an activity from the mobile module. So far, I couldn't do that. IF I install the mobile version AND the tv version, the intent solution works, but I cannot afford to do two installations. – Rauter Oct 18 '16 at 14:16
  • aha I get it sorry I didn't get that from the start but the issue is legit you are trying to launch something that doesn't exist you need to put it inside the tv app or tell the user to download the other app for this function like foursquare/swarm thing – Hala.M Oct 18 '16 at 14:19
  • The mobile module is a dependency of tv module, but that doesn't seem to be enough. Maybe you are right: the activity that I want to launch is not included in the tv module. I know that this would work if the mobile module was a library and not an application. – Rauter Oct 18 '16 at 14:24
  • 1
    yes exactly sorry I am not much of a helper but sometimes talking about it solves it :D – Hala.M Oct 18 '16 at 14:29
  • Since both are diff app.The IntentFilter implement will be ur only solutn – Rissmon Suresh Oct 18 '16 at 15:49
  • But that won't work without installing both application modules. What is the point of being able to have multiple applications modules in the same package and/or project if they can't share code? – Rauter Oct 18 '16 at 16:17

0 Answers0