Search⌘ K
AI Features

Persist Login State

Explore how to maintain user login state in an Android app using SharedPreferences. This lesson teaches you to check login status, store preference data, and navigate users accordingly to create a smoother login flow.

Flow overview #

When LoginActivity is opened, we need to check the login state:

  • If the user is logged in already, we close LoginActivity and open MainActivity straightaway.
  • If the user is not logged in, we proceed to the regular flow and save the login state in the end.

Shared preferences #

In Android, to store a simple key-value data SharedPreferences can be used. This storage support saving the following types of data:

  • String
  • boolean
  • int
  • long
  • float
  • Set<String> values
...