Rafael
Rafael

Reputation: 1487

Using activities from library

I'm creating my first library to use in some future projects, on it I'm creating some Activities that are the same on every project.

Right now I'm working with a test project and on my library I have this LoginActivity. It has it's own layout and works fine.

How can I make my test app LoginActivity be the one from the library? At the moment I'm extending my LoginActivity from the library into my activity on the project. Is this the right way of doing it considering that all the code logic happens on the Library activity?

Library LoginActivity

public class LoginActivity extends AppCompatActivity {

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

        // Some useless code...

    }
}

Project Login Activity

public class MainActivity extends LoginActivity {

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

        // Nothing happens in this class... really...

    }

}

Upvotes: 0

Views: 109

Answers (2)

cincy_anddeveloper
cincy_anddeveloper

Reputation: 1152

Make sure that Activity that is inside of the library project is declared inside of the library's manifest file. Then start the activity how you normally would start any other activity. If you want the activity to be the first activity when the app launches (launcher activity), you need to declare it explicitly inside of your manifest and add an intent-filter tag element as a child, that indicates it should be the launcher activity. View this post.. Another way to roughly achieve the same effect would be to create a blank activity that is declared in the app manifest file as the launcher activity and inside of the onCreate method launch the library activity. Make sure you clear any and all activity transitions so that the switch to the library activity is seamless. The benefit to this approach is that you can perform a check before launching the library activity.

Ex. checking if the login activity needs to be displayed or if the screen can be by-passed and the app can be entered immediately.

Upvotes: 1

bvarga
bvarga

Reputation: 723

Add login activity into AndroidManifest.xml in library or application.

<application>
        <activity
            android:name="com.mypackage.LoginActivity" />

</application>

Upvotes: 1

Related Questions