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.
A possible approach:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:statusBarColor">@color/background_splash</item>
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
<?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>
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With