I’m currently building out a ICS 4.0+ Android app that will require authentication. The issue I’m having is handling checking for login status when:
- User first opens the app
- User returns to app after closing (resuming)
The behavior I’m looking for is that there is a “loading…” splash screen if the app is removed from memory and reopened, and then an almost unnoticeable splash screen when the user returns to app upon resuming (still in memory).
The code I currently have for checking this is:
public static void checkUserStatus(Context context, boolean isPassive) {
Log.d(TAG, "Checking user status..");
// User is logged in and has cc
if ( APIUtil.isLoggedIn() && UserAccount.getCreditCards().size() > 0 && isPassive == false) {
Intent intent = new Intent(context, OtherActivity.class);
context.startActivity(intent);
// User is logged in but has no cc
} else if (APIUtil.isLoggedIn() && UserAccount.getCreditCards().size() == 0) {
Intent intent = new Intent(context, ManageCCActivity.class);
context.startActivity(intent);
// User is not logged in
} else if(isPassive == false) {
Intent intent = new Intent(context, HomeActivity.class);
context.startActivity(intent);
}
}
checkUserStatus is called in onResume() of every activity. The idea being that it’ll check this and redirect accordingly, but the behavior is wacky and inconsistent and it just feels janky.
Are there any examples of authentication flow out there? Ideas? Suggestions?
A pattern that I’ve used and seen other use that works well looks something like this:
Service, and then run thatServicebound (meaning you never start or stop it, you bind to it…docs link for more on that).Activityimplementation that binds to the service inonStart()and unbinds froms it inonStop(). This allows theServiceto stay running as you move from oneActivityto another. Have anyActivitythat needs access to this information inherit from this base (which may be everyActivity).Servicecheck the information it needs to only when it starts up, and call back with the results.What this creates is a (fairly) single instance of checking for authorization that only checks when the application starts up or the user returns after leaving it, without constantly checking as you move from one
Activityto another. It also allows it to gracefully finish any long-running operations if the user leaves prematurely since all the work is being done in aService.