OnCompleteListener 在另一个 class 中获取结果
OnCompleteListener get results in another class
我有一个 MyActivity
,它由 FragmentA
、FragmentB
和 MyActivityPresenter
组成。
片段A:
@OnClick(R.id.proceed_sign_up)
public void onBtnProceedSignUp(){
if(checkInputSignUp()){
int returnCode = presenter.createAccount(email, pswd, nick);
//handle return code: show error on EditTexts,etc.
}
}
主持人:
public int createAccount(String email, String password, final String nickname) {
final int[] code = new int[1];
activity.showLoading(true);
firebaseUserService.createUserWithEmail(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
processLogin(task.getResult().getUser(), task.getResult().getUser().getProviderData().get(1), nickname);
} else {
Log.d("Create not success", task.getException().toString());
switch (task.getException().getMessage()) {
case "The email address is already in use by another account":
code[0] = 10;
break;
default:
code[0] = 20;
break;
}
}
activity.showLoading(false);
activity.showLoginFail();
}
});
return code[0];
}
因为操作是异步的,当我尝试从 Fragment
中的方法 createAccount()
获取 returnCode 时,Task<AuthResult>
未完成(所以我总是得到 0)。在任务完成或以某种方式重组代码后获得价值的最佳方法是什么?
最好的方法是在您的视图中调用一个方法,就像您在调用 showLoading 和 hideLoading 方法时所做的那样:
public void createAccount(String email, String password, final String nickname) {
final int[] code = new int[1];
activity.showLoading(true);
firebaseUserService.createUserWithEmail(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
processLogin(task.getResult().getUser(),
task.getResult().getUser().getProviderData().get(1), nickname);
} else {
Log.d("Create not success", task.getException().toString());
switch (task.getException().getMessage()) {
case "The email address is already in use by another account":
code[0] = 10;
break;
default:
code[0] = 20;
break;
}
activity.showResult(code[0]);
}
activity.showLoading(false);
activity.showLoginFail();
}
});
}
并在您的 activity 实现界面中添加:
void showResult(int resultCode);
最后,在实现接口的 activity/fragment 中,您需要重写:
@Override
public void showResult(int resultCode){
//do something with resultCode
}
我有一个 MyActivity
,它由 FragmentA
、FragmentB
和 MyActivityPresenter
组成。
片段A:
@OnClick(R.id.proceed_sign_up)
public void onBtnProceedSignUp(){
if(checkInputSignUp()){
int returnCode = presenter.createAccount(email, pswd, nick);
//handle return code: show error on EditTexts,etc.
}
}
主持人:
public int createAccount(String email, String password, final String nickname) {
final int[] code = new int[1];
activity.showLoading(true);
firebaseUserService.createUserWithEmail(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
processLogin(task.getResult().getUser(), task.getResult().getUser().getProviderData().get(1), nickname);
} else {
Log.d("Create not success", task.getException().toString());
switch (task.getException().getMessage()) {
case "The email address is already in use by another account":
code[0] = 10;
break;
default:
code[0] = 20;
break;
}
}
activity.showLoading(false);
activity.showLoginFail();
}
});
return code[0];
}
因为操作是异步的,当我尝试从 Fragment
中的方法 createAccount()
获取 returnCode 时,Task<AuthResult>
未完成(所以我总是得到 0)。在任务完成或以某种方式重组代码后获得价值的最佳方法是什么?
最好的方法是在您的视图中调用一个方法,就像您在调用 showLoading 和 hideLoading 方法时所做的那样:
public void createAccount(String email, String password, final String nickname) {
final int[] code = new int[1];
activity.showLoading(true);
firebaseUserService.createUserWithEmail(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
processLogin(task.getResult().getUser(),
task.getResult().getUser().getProviderData().get(1), nickname);
} else {
Log.d("Create not success", task.getException().toString());
switch (task.getException().getMessage()) {
case "The email address is already in use by another account":
code[0] = 10;
break;
default:
code[0] = 20;
break;
}
activity.showResult(code[0]);
}
activity.showLoading(false);
activity.showLoginFail();
}
});
}
并在您的 activity 实现界面中添加:
void showResult(int resultCode);
最后,在实现接口的 activity/fragment 中,您需要重写:
@Override
public void showResult(int resultCode){
//do something with resultCode
}