Skip to content Skip to sidebar Skip to footer

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 proje

Solution 1:

When you do this in your library project

 Intent i = newIntent(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:

publicvoidrunMyActivity() {
    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
publicvoidrunMyActivity() {
    // 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).

Solution 2:

In your new project, you could use a different name, instead of AndroidActivity. If you don't want to do that, the second AndroidActivity's fully qualified name in the manifest file should solve your problem. You are perhaps messing up with packages a little bit, somewhere.

activity android:name="de.app.library.activities.AndroidActivity"

should be

activity android:name="de.app.free.activities.AndroidActivity"

I meant, the AndroidActivity. In some way, your are picking up the wrong activity class from the wrong package.

Post a Comment for "Starting Activity From Android-library-project"