Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Activity from Android-Library-Project

after a long search I couldn't find any appropriate solution. I have a Android-Library Project with nearly all code for the application. From the main activity in the library project I start an Intent to en explicit Activity A. In my derived Project, which uses the library Project, I have extended this Activity A and added some new code. The problem is that the the Superclass of Activity A will respond and not the derived class.

In the manifest of the new Project which uses the library project I have declared the new Activity with the new package.

Here is the Intent call from the Library Project:

Intent i = new Intent(getApplicationContext(), AndroidActivity.class);
startActivity(i);

Here is the derived AndroidActivity class:

public class AndroidActivity extends de.app.library.activities.AndroidActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
            ...

I am not able to get to the onCreate Method

The Manifest from the Library Project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.app.library"
android:versionName="1.0" 
android:versionCode="1">

<uses-sdk android:minSdkVersion="4" />

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">

    <activity android:name=".activities.MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".activities.AndroidActivity" android:label="@string/act_android" />
</application>

And here the Manifest from the new Project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="de.app.free"
  android:versionCode="1"
  android:versionName="1.0">

<uses-sdk android:minSdkVersion="4" />

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">


    <activity android:name="de.app.library.activities.MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".activities.LernenActivity" android:label="@string/act_lernen" />

So I have:

de.app.library.activities.AndroidActivity  and
de.app.free.activities.AndroidActivity

I didn't want to do that much changes to the library project beacuase a third project should use the existing code with the untouched AndroidActivity

ALl other things works fine for me. For example the changed layouts are used from the new project.

How can I handle that problem? Maybe changes in the Manifest from the new project in which I declare, that the new activity should be called instead of the superclass?

Thanks in advance!

like image 764
treimy Avatar asked Aug 07 '11 18:08

treimy


People also ask

How do I open Android apps from library?

You can see all the apps you've ever downloaded on your Android phone by opening the "My apps & games" section in your Google Play Store. The apps you've downloaded are divided into two sections: "Installed" (all the apps currently installed on your phone) and "Library" (all the apps that aren't currently installed).

Which method is use for start activity in Android?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

How do I open an activity from an existing activity?

Start new Activity by existing Activity on a button clickUsing the "startactivity(Intent intent)" method and "startActivityforResult(Intent intent, requestCode requestCode)" method.

Which activity is called first in Android?

Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.


1 Answers

When you do this in your library project

 Intent i = new Intent(getApplicationContext(), AndroidActivity.class);

AndroidActivity refers to the library project's AndroidActivity: A project doesn't know about other external projects unless you include them as a library. Just check your imports, you are importing AndroidActivity, and instructing Android to run it.

Which by the way it's obvious, it would be a bad design pattern if the library project had to know about the derived projects

An easy solution is to override the activity launching on the derived project, something like this:

Libary project Activity:

public void runMyActivity() {
    Intent i = new Intent(getApplicationContext(), AndroidActivity.class);
    // You are on the library project, so you can refer to the activities created in it.
    // the only AndroidActivity known here is the library project's
    .
    .
}

Your derived project's Activity:

@Override
public void runMyActivity() {
    // You are now on the derived project, so you can refer to 
    // the activities created in it, and also to the library project's. You
    // just import the package name of the desired activity or use fully qualified
    // names

    Intent i1 = new Intent(getApplicationContext(), AndroidActivity.class);
    // this one will use the activity in the imports
    Intent i2 = new Intent(getApplicationContext(), de.app.libarary.AndroidActivity.class);
    // use the activity from the library project
    Intent i3 = new Intent(getApplicationContext(), de.app.free.AndroidActivity.class);
    // use the activity from the derived project

    .
    .
}

So when you call runMyActivity() from anywhere, it will execute the overriden function ( provided the startup activity extends the library project's activity and overrides that method). And in the overriden function context, AndroidActivity.class will be your derived activity (or the other one, you can import any of them because here you have access to the derived activities AND the library project's).

like image 80
rupps Avatar answered Sep 19 '22 11:09

rupps