Beginner
Beginner

Reputation: 1424

Calling an activity inside a android library module

I have an android library module and I'm trying to start an activity like

 Intent intent = new Intent(mContext, DetailsScreen.class);
            mContext.startActivity(intent);

I'm making above request inside the module and I have referenced the module in app gradle file like compile project(':myModule')

Also i have defined activity in Manifest file of both app module and in myModule like

  <activity
            android:name="com.test.mymodule.DetailsScreen" >
            <intent-filter>
                <action android:name="com.test.mymodule.DetailsScreen" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

But the activity which opens is an blank activity.

Can some one kindly explain me what's wrong I'm doing ?.

Thanks in advance :) :)

Upvotes: 10

Views: 5491

Answers (3)

Teja
Teja

Reputation: 807

This ans is copied from siddhesh

We can use reflection to get class object.

Class.forName("com.mypackage.myMainActivity")

Add this code in Library project to call,

try {
      Intent myIntent = new Intent(this,Class.forName("com.mypackage.myMainActivity"));
      startActivity(myIntent );
} catch (ClassNotFoundException e) {
     e.printStackTrace();
}

"com.mypackage.myMainActivity" is the Activity present in Main project, that we need to call from its Library project.

Upvotes: 1

Sree Reddy Menon
Sree Reddy Menon

Reputation: 1311

you should mention only your library activity in app manifest. like how we include for facebook or other sdk activities. and start the activity with intent from your app. just try with removing activities from manifest. include only on app module.(package must be from library's )

Upvotes: 2

Federico Picci
Federico Picci

Reputation: 1113

Right click on App module, then Open Module Settings, choose App from the left, and on the last tab add the Module dependecy to your lib (you don't need to edit gradle file this way, even if what you have in your gradle seems correct). Then, declare the activity you want to open only in the android manifest of the library module.

<activity android:name=".myLibActivity"/>

Upvotes: 0

Related Questions