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.