Android: Start different activity if there is a user logged in

Viewed 871

I am building an Android application where one activity is a login screen. When the app is opened, if a user has already logged in, I would like to skip the LoginActivity and have the user be directed to another one. When a user logs into my app (using Google Firebase), I save their username and other data in their device's shared preferences. When they log out, their shared preferences are cleared.

The way I currently have my manifest file is such that the only activity that can be opened when the app is started is the LoginActivity:

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

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

In the LoginActivity's OnCreate() method, if there is a username saved in the shared preferences (meaning a user is logged in), I immediately change activities:

public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences userData = getApplicationContext().
                getSharedPreferences("userdata", 0);
        String n = userData.getString("username", "");
        if (!userData.getString("username", "").equals(""))
        {
            Intent myIntent = new Intent(LoginActivity.this, TabbedActivity.class);
            startActivity(myIntent);
        }
}

However, there is a problem with this approach. Many times, the LoginActivity is still shown for a split second before starting the TabbedActivity. I would like to fix this so that the LoginActivity is actually never seen at all if a user is logged in.

I assume that the approach I'm taking is all wrong and there is a much cleaner way of doing it such that the correct activity is immediately opened. Any help on this would be greatly appreciated.

3 Answers

A possible approach:

  1. Create a style for splash theme:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:statusBarColor">@color/background_splash</item>
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>
  1. Create a background drawable (drawable/background_splash.xml) for splash:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/background_splash"/>
    </item>
    <item>
        <bitmap
            android:src="@drawable/ic_splash_logo"
            android:gravity="center"/>
    </item>
</layer-list>
  1. In your manifest, set the SplashTheme as your application/launcher-activity theme:
<application
    ...
    android:theme="@style/SplashTheme">

<!-- or -->

<activity
    ...
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

  1. In your LoginActivity in onCreate() set your normal app theme and content view if the user is not logged in and also set app theme in the MainActivity (or set it in the Manifest)
public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        val isUserLoggedIn = ...
        if (!isUserLoggedIn)
        {
            setTheme(R.style.AppTheme)
            setContentView(R.layout.activity_login)
        } 
        else {
            //Navigate to Main Activity
        }
    }
}

Splash screen reference

I'm not sure if this is the best approach, but you could create a Loading activity that start the activity needed in any situation like.

public class LoadingActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences userData = getApplicationContext().
                getSharedPreferences("userdata", 0);
        String n = userData.getString("username", "");
        if (!userData.getString("username", "").equals(""))
        {
            Intent myIntent = new Intent(LoginActivity.this, TabbedActivity.class);
            startActivity(myIntent);
        }else{
            Intent myIntent = new Intent(OtherActivity.this, TabbedActivity.class);
            startActivity(myIntent);
        }
}

And about the view add a gif or a logo meanwhile.

I have an implementation, but no shared preferences, and I don't see the login screen at any time.

My structure is as follows: I have an initial splash screen, then a main activty, which is where I override the onStart method to check if the user has an open section if it is passed to the home activity.

I hope it helps you, if you don't tell me and I'll add more code.

public override fun onStart() {
    super.onStart()
    // if user is loged goto Home Activity
    if (firebaseUser != null) {
        // Name, email address, and profile photo Url
        val name = firebaseUser?.displayName
        val email = firebaseUser?.email
        val photoUrl = firebaseUser?.photoUrl
        val uid = firebaseUser?.uid
        val emailVerified = firebaseUser!!.isEmailVerified
                  
        goToActivity<HomeActivity>()
    }
}
Related