Invoking activity inside the Android library module

I have an android library module and am trying to start activities like

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

I make the request above inside the module and I refer to the module in the app gradle file, for example compile project(':myModule')

I also defined activity in the manifest file of both the application module and myModule, for example

  <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 being discovered is an empty activity.

Can someone explain to me what I'm doing wrong?

Thanks in advance:):)

+8
android android-intent module android-studio
source share
3 answers

you should indicate only your activity l in the application . as we include for facebook or other sdk actions. and start your activity by evading your application. just try removing the actions from the manifest. include only in the application module (the package must be from the library)

+1
source share

Right-click on the App module, then open the module settings, select the application on the left, and on the last tab add the dependecy module to your library (you do not need to edit the gradle file this way, even if it seems right in your gradle). Then declare the action you want to open only in the android manifest of the library module.

 <activity android:name=".myLibActivity"/> 
0
source share

This answer is copied from siddhesh

We can use reflection to get a class object.

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

Add this code to the library project to invoke,

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

"com.mypackage.myMainActivity" is the action present in the main project that we need to call from its library project.

0
source share

All Articles