0

I am trying to start an activity from another module in the same project. I use Retrofit Client and at onResponse I want to start the main activity from another module. I imported the module in my project with "Import new module from Android Studio Bar"

I tried to use Intent with Class.forName() but it doesn't work, I also tried to add in the manifest the activities. I have the project Lab that contains two modules: app(com.example.lab) and home(he.kome.lis) modules. When I tried to add in app-manifest the activity from home it is red and I get "Unresolved class".

This is how my manifest for app looks like:

<activity android:name="com.example.lab.LogInActivity">
<intent-filter>
    <action android:name="android.intent.action.MAIN"/>

    <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="he.kome.lis.AddActivity">
</activity>
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Mary
  • 103
  • 2
  • 11

2 Answers2

1

First, you need to make the imported module as a library module by changing its module build.gradle plugin from:

apply plugin: 'com.android.application'

to:

apply plugin: 'com.android.library'

Second, if you need to hide the implementation details of how to call the Activity modules you can use a Navigator singleton class inside your library module:

public class Navigator {
    private Navigator(){}

    public static Navigator getInstance() {
        return NavigatorHolder.INSTANCE;
    }

    private static class NavigatorHolder {
      private static final Navigator INSTANCE = new Navigator();
    }

    public void navigateToMainActivity(Context context) {
      Intent intent = new Intent(context, LibrariesMainActivity.class);
      context.startActivity(intent);
    }
}

Then you can launch the MainActivity of library module with:

Navigator.getInstance().navigateToMainActivity(this);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

It seems you have not properly added them as your dependency Go to project and right click and then select open module settings go to dependencies tab and click '+' button on right side (beside scope label) and click modules option and select required dependency click OK