Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :) :)

like image 906
Beginner Avatar asked Feb 19 '16 14:02

Beginner


People also ask

Which method is used to call another activity in Android?

public void displayName() { String name = fetchName(); // Here's the call. System.

How can I run a particular activity in Android Studio?

Go to "Edit Configurations..." in the "Run" menu. In the left pane, select your application. In the right pane, in the "General" tab, in the "Launch Options" section, there is a "Launch:" dropdown. Select "Specified Activity", and enter the name of your activity as it appears in your Manifest.

What is Android library module?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.

Which method starts or calls another activity?

Start the Second 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 .


2 Answers

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 )

like image 54
Sree Reddy Menon Avatar answered Oct 03 '22 00:10

Sree Reddy Menon


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.

like image 45
Teja Avatar answered Oct 03 '22 00:10

Teja