user-44651
user-44651

Reputation: 4124

Inherited Activity methods are not being called from an imported library

I'm trying to take my Java skills up a notch by learning to create a library of shared code/actives/components etc..

A couple of my apps all use the same Login Screen Activity and UI so I am trying to build a "Base Activity" class to inherit from in my TestApp so I can quickly reuse code.

When the application starts up the Login Screen is presented from the BaseLoginActivity but nothing in the LoginActivity is being called or overridden.

So when I click the login button moveToDesiredIntent is only called in the BaseLoginActivity.

All the "Base" classes are in the Library and my Child classes are in my TestApp.

I've read the Google docs on inheritance and I thought I was doing this correctly. I started with abstract BaseClasses but then I would get null pointers or not concrete classes.

What is the proper way for me to inherit from my base classes so I can reuse code?


Code Below

I built a BaseActivity in my shared library

public class BaseActivity extends AppCompatActivity {
    protected String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Then I build the BaseLoginActivity in my library and in my test app a LoginActivity that inherits from that. The BaseLoginActitivy has a button with an onclick listener set

BaseLoginActivity

public class BaseLoginActivity extends BaseActivity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.base_activity_login);
               Log.d(TAG, "onCreate:");
               Button button = findViewById(R.id.btnLogin);
               button.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     moveToDesiredIntent();
                 }
                });

            }

            protected  void moveToDesiredIntent();
    }

LoginActivity THIS IS THE ACTIVITY THAT ISN'T GETTING ITS METHODS CALLED

public class LoginActivity extends BaseLoginActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate:"
    }

    @Override
    protected void moveToDesiredIntent() {
        super.moveToDesiredIntent();
        Intent intent = new Intent(this, DashboardActivity.class);
        startActivity(intent);

        finish();
    }
}

And finally, where you should go when you have logged in

BaseDashboardActivity

public class BaseDashboardActivity extends BaseActivity {
}

DashboardActivity

public class DashboardActivity extends BaseDashboardActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Manifest of my TestApplication

<application //...>
    <activity android:name=".DashboardActivity" />
    <activity android:name=".LoginActivity">    
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>
</application>

Manifest of my Library

<application //....>
    <activity android:name=".ui.base.BaseActivity" />
    <activity android:name=".ui.shared.BaseLoginActivity" />
    <activity android:name=".ui.shared.BaseOnBoardingActivity" />
</application>

Upvotes: 0

Views: 31

Answers (1)

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37584

You are missing the setContentView in your DashboardActivity. If no layout gets inflated there is nothing to show. I would also suggest to make your base classes as abstract and provide abstract methods.

Upvotes: 1

Related Questions