0

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...

    }

}
Rafael
  • 1,437
  • 6
  • 23
  • 41
  • Presumably you will have other activities that extend LoginActivity. What common functionality will be needed such that the subclasses need to have LoginActivity as a parent class? – Al Lelopath Feb 15 '17 at 18:07
  • Mostly it's because I'll use it in two different projects, It will handle the authentication process with the online server, so instead of having it and another classes that will be identical on multiple projects, I'm trying to create a library with them. – Rafael Feb 15 '17 at 18:13
  • So then, why not use LoginActivity directly? Since you are setting the layout in the parent class, the layout is the same regardless of the project. I guess I'm hesitant to have an activity like that in a library. It sounds like you could create a utility class library that handles the authentication process. – Al Lelopath Feb 15 '17 at 18:15
  • Yes, the layout is the same, but I don't know how to use the Activity from the Library directly. How can I do this? – Rafael Feb 15 '17 at 18:17
  • I wouldn't have the activity in the library. In each project have `public class MainActivity extends AppCompatActivity`. Then have a library project, call it `LoginUtilities` maybe and have a method `public static boolean authenticate(...)` and each project's `MainActivity` calls this method. – Al Lelopath Feb 15 '17 at 18:23
  • If you're intent (ha!) on having a library with an Activity, I would read [these SO questions](https://www.google.com/webhp?source=search_app#q=android+activity+in+library+project). – Al Lelopath Feb 15 '17 at 18:38

2 Answers2

1

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

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

</application>
bvarga
  • 716
  • 6
  • 12
1

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.

Community
  • 1
  • 1
cincy_anddeveloper
  • 1,140
  • 1
  • 9
  • 19