What is the correct way of creating a login screen/activity in Android?
android, android-activity, authentication
Solution
What I ended up doing was to make my Home activity handle the intent android.intent.action.MAIN. Home activity, when launched, checks if the user is signed in or not (using shared preferences), if its not then it starts LoginActivity and calls finish() on its self.
LoginActivity on successful login starts the Main activity and this time because the user is logged on, the Main activity will continue its normal course. LoginActivity is declared as following in the manifest file:
<activity android:name="LoginScreen" android:label="@string/app_name"
android:noHistory="true" android:excludeFromRecents="true">
</activity>
Setting noHistory and excludeFromRecents to true for LoginActivity means that the user cant return to this activity using back button.
Problem
I am working on an Android application that requires a user to login before doing anything else. Currently I have created main Activity named LoginScreen and upon successful login this activity launches another Activity called Home. But I see a problem with this approach. What if user presses back button from Home activity? I dont want user going back to login screen. what is the correct way of stopping the user from doing that. Do I need to handle Key Press events?