Apostrofix
Apostrofix

Reputation: 2180

How to extend a class from main application in an android library activity?

I've used this answer: Same Navigation Drawer in different Activities to implement a navigation drawer that is shown in all activities.

I am not sure if this is the right way to go, but at the moment I have a main application, that contains some activities and implements the NavigationDrawer in the BaseActivity.

I would like to start by creating many android library projects that will have activities/fragments in them and their own logic. So for now, besides the main application, I've created one android library project and I can successfully start one of its' activities from the main application (from the NavigationDrawer).

The problem is that I cannot make it extend the BaseActivity and therefore the NavigationDrawer is not shown.

So, at the moment in the main application, I have:

activity_main.xml // it is the launcher activity
activity_one.xml
activity_two.xml
MainActivity.java
OneActivity.java
TwoActivity.java

That works fine. Now I want to add the library project into play, in which I have:

activity_three.xml
ThreeActivity.java

And now the problem is that in the library project, I cannot extend from the BaseActivity like that:

public class ThreeActivity extends BaseActivity{ // <-- There's an error here: Cannot resolve symbol 'BaseActivity'

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
    }
}

Does anybody have any suggestions how to do that?

Should I just create another library project that has the BaseActivity and the navigation drawer ?

Upvotes: 1

Views: 927

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006809

Should I just create another library project that has the BaseActivity and the navigation drawer ?

Either that, or get rid of all the libraries and move everything into the one project. A library cannot inherit from classes from a project that consumes the library. A library can inherit from classes from a project that the library itself depends upon, though.

So, the app/ module could depend upon the three/ library (that has ThreeActivity), which in turn depends upon the base/ library (that has BaseActivity).

Upvotes: 2

Related Questions