Java classes and interface combination

白昼怎懂夜的黑 提交于 2021-01-27 12:53:17

问题


I'm trying to create couple of Java class to perform certain work. Let's say I want to get the task done by calling my classes like this:

FirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");

                        FirebaseUser user = task.getResult().getUser();
                        // ...
                    } else {
                        // Sign in failed, display a message and update the UI
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                        }
                    }
                }
            });

I could understand up to signInWithCredential(). I can't figure out how to implement addOnCompleteListener() and have a interface as argument.

I've currently create my top class like FirebaseAuth with methods like getInstance () and signInWithCredential(). Also, I tried creating an interface but I am getting error that result of the interface is never used. How can I implement the style of addOnCompleteListener(parameter 1, interface 2).

Here, addOnCompleteListener is getting parameters of activity and interface and in my case, I will be using the activity parameter for some work.

P.S: I found out this is called interface callback. If it's right, any guidance to it's structure will be great


回答1:


You can do it like this:

Create an interface:

public interface onCompleteListener {
    void onComplete(ThrwAtTask object);
}

Define your ThrwAtTask:

public abstract class ThrwAtTask {

    public abstract boolean isSuccessful();

    public abstract String getUser();

    public abstract String getOtherData();

}

In your main class:

public class ThrwAt{

    public static ThrwAt instance;
    private static Activity mActivity;
    public com.basusingh.throwat.onCompleteListener onCompleteListener;


    private ThrwAt(Activity activity) {
        mActivity = activity;
    }

    public static synchronized ThrwAt getInstance(Activity activity) {
        if (instance == null) {
            instance = new ThrwAt(activity);
        }
        return instance;
    }

    public void addOnCompleteListener(@NonNull onCompleteListener var2) {
        onCompleteListener = var2;
        //Call your task function
        doTask();
    }

    public void doTask(){
        ThrwAtTask o = new ThrwAtTask() {
            @Override
            public boolean isSuccessful() {
                return true;
            }

            @Override
            public String getUserId() {
                return "userId1";
            }

            @Override
            public String getApiKey() {
                return "apiKey";
            }
        };
        //Once done, pass your Task object to the interface.
        onCompleteListener.onComplete(o);
    }

}

Usage:

ThrwAt.getInstance(MainActivity.this).addOnCompleteListener(new onCompleteListener() {
            @Override
            public void onComplete(ThrwAtTask object) {
                doYourWork(object);
            }
        });


来源:https://stackoverflow.com/questions/63740596/java-classes-and-interface-combination

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!