0

OK, I have a login class like the snippet below

public class LoginClass {

    public void login() {
        login.authorize(username,password); 
            //run activity specific code here
    }

}

This class is quite generic, but depending on which activity calls LoginClass.login(), I would like different actions to be performed.

I've seen some implementation where a function is passed into the class and this function is then run on completion. Can anyone give me a brief example on how to do this.

totalitarian
  • 3,606
  • 6
  • 32
  • 55
  • 1
    my first guess would be a callback, something like this: [http://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android][1] [1]: http://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android – sschrass Aug 17 '12 at 14:08
  • 1
    @user1565982: you can look at the answer I've posted for the same question: http://stackoverflow.com/questions/10776764/what-is-the-right-way-to-communicate-from-a-custom-view-to-the-activity-in-which/10776818#10776818 . I've shown there how to implement listener for a View derived class - but it's completely the same implementation. just switch the word "view" with "LoginClass", and you've got your answer – Tal Kanel Aug 17 '12 at 14:35

3 Answers3

2

Functions are not first class objects in Java as seen on Android - you cannot pass a function. The ideology of Java calls for deriving from your generic class (possibly anonymously) when it needs to be specialized. Like this:

class MyActivity
{

    void onCreate()
    {
        MyButton.addOnClickListener(
            new LoginClass()
            {
                //Just like LoginClass, but with one method overridden
                public void login()
                {
                    super.login(); //Call the base
                    ThenSomethingElse();
                }
            });
    }
}

Alternatively, you can create hooks in the login class - functions that are called whenever custom action is needed - and override those in the activity. Such is the Java way.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Accepting this answer as is was the most complete explanation on how to handle callbacks. In the end I decided to go with implementing an interface – totalitarian Aug 17 '12 at 16:24
1

You could use Handlers to provide a callback mechanism.

Define a handler in your caller

Handler mHandler = new Handler()
{
 public void handleMessage(Message msg)
 {
 }
}

Pass your handler to the login function:

LoginClass.login(mHandler);

Then send the handler a message:

public void login(Handler handler)
{
 handler.sendEmptyMessage(0);
}
CSmith
  • 13,318
  • 3
  • 39
  • 42
1
public class LoginClass {

    public void login(boolean b) {
        login.authorize(username,password); 
          if (b){
                 //do something
            }
          else {
                //do something
             }
    }

just use constructor argument to do this.

Class 1
LoginClass loginClass = new LogonClass(false)

Class 2
LoginClass loginClass = new LogonClass(true)
fish40
  • 5,738
  • 17
  • 50
  • 69
  • I really can't do it this way as the class I'm creating will be for an api. So users who call that class will be writing their own functions – totalitarian Aug 17 '12 at 14:48