0

I have an app that sometimes may require a login in the middle of execution, and I'm tyring to figure out how to pass around control. There are quite a number of different things to do for authentication, so I have a utility class called Authenticator taking care of all that.

In short, I want it so all activites in the app will be able to call some method like Authenticator.login() to present a login screen, and have Authenticator take care of everything.

Here's what I've thought of so far for the login method:

public boolean login(Activity context)
{ 
    Intent intent = new Intent(context, Login.class);
    context.startActivityForResult(intent, LOGIN_ACTIVITY);
    return false;
}

The problem I'm running into, though, is getting the result back from logging in. I want to know if they pressed the cancel button or had an incorrect password BEFORE I pass control back to the Activity that called login().

There is the method onActivityResult(), but that is in class Activity, whereas Authenticator is not an activity. Is there a way to start an activity for a result from inside a normal java class?

Indigenuity
  • 9,332
  • 6
  • 39
  • 68
  • Can you clarify it more, the screen where you ask your user to enter Username and Password is an activity ? – Gopal Jun 09 '11 at 18:53
  • Yes, the login screen is an activity. Other activities in the application that need the user to be logged in ask the Authenticator to take care of that. Authenticator then opens the login activity if they aren't already logged in. – Indigenuity Jun 09 '11 at 19:04
  • If Login Screen is an activity, then you can setResult what you got from Authenticator class and send it back to calling activity. And do accordingly in your calling activity. This link explains you how to pass result from current activity to previous activity. [LINK](http://stackoverflow.com/questions/1124548/how-to-pass-the-values-from-one-activity-to-previous-activity). Hope this help. – Gopal Jun 09 '11 at 19:12
  • I would do this, but the results from the LoginScreen don't get sent back to the Authenticator through onActivityResult() because it is NOT an Activity, so it doesn't have that method or listener. To see what I ended up doing, see below for my answer. – Indigenuity Jun 13 '11 at 16:56

2 Answers2

1

In your example, the Activity passed in as context will get the callback when the login completes. I don't think there is a way to have that callback occur in a non-activity class.

One option would be to create a base Activity, AuthenticatedActivity that all of your activities extend from. Its a bit of a heavy-handed solution, but it would allow you to handle all of the authorization work in one place.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
0

Though it's not an elegant solution, here's what I did in the end:

To not couple the login screen with the activities using the Authenticator, instead of starting the activity(login screen) for a result, I used the onFinish() method of the login screen to call a method in the Authenticator to pass back results.

Any Activity that asks the Authenticator to login now also implements an interface with one method:

public void onLoginComplete(boolean successful);

The Authenticator then replies to the calling Activity using this method.

Though this wasn't the easiest way to solve the problem, the Activities in my program are now decoupled from the login screen and only interact with the Authenticator.

Indigenuity
  • 9,332
  • 6
  • 39
  • 68