What is the correct way of creating a login screen/activity in Android?

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?


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.


After you call startActivity(...) in the LoginScreen activity, call finish(). This will remove that activity from the activity stack, so pressing back will essentially close your app once you're in your Home activity.


Try setting flags to the Intent.

Example:

new Intent(context, SomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

More information on flags: http://developer.android.com/reference/android/content/Intent.html#nestedclasses


See: https://stackoverflow.com/a/41290453/4560689 (text below)

To do this, you should create a single launcher activity with No Display (using Android's NoDisplay theme), that runs the logic of whether to go to the home screen or to log in/register.

First, in your manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android">

<-- Permissions etc -->

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">

    <activity
        android:name=".onboarding.StartupActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:theme="android:style/Theme.NoDisplay">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop" />

    <activity
        android:name=".authentication.controller.AuthenticationActivity"
        android:label="@string/title_sign_in"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize|stateHidden" />

    <-- Other activities, services, etc -->
</application>

Then, your StartupActivity:

package com.example.android.onboarding;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.example.android.MainActivity;

import com.example.android.authentication.controller.AuthenticationActivity;

import com.example.android.util.ResourceUtils;

public class StartupActivity extends Activity {
    private static final AUTHENTICATION_REQUEST_CODE = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (isLoggedIn()) {
            Intent startupIntent = new Intent(this, MainActivity.class);
            startupIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(startupIntent);
            finish();
        } else {
            Intent startupIntent = new Intent(this, AuthenticationActivity.class);
            startActivityForResult(startupIntent, AUTHENTICATION_REQUEST_CODE);
        }

        super.onCreate(savedInstanceState);
    }

    private boolean isLoggedIn() {
        // Check SharedPreferences or wherever you store login information
        return this.getSharedPreferences("my_app_preferences", Context.MODE_PRIVATE).getBoolean("loggedIn", false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == AUTHENTICATION_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            Intent startupIntent = new Intent(this, MainActivity.class);
            startupIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(startupIntent);
        }

        finish();
    }
}

Gist here: https://gist.github.com/chanakin/c44bf1c6a9a80d2640440b5aaa92c8ee