英文:
how to call onActivityResult in non Activity class android
问题
我创建了一个 Fragment,当点击按钮时会弹出类似对话框的弹出视图。在那个视图中包含了 Google 登录按钮。当我连接到 Firebase 并在 startActivityForResult 后继续实现时,我需要重写 onActivityResult 方法。但是弹出视图对应的类是另一个继承自 Dialog 类的类,无法重写 onActivityResult 方法。当尝试重写时会显示 method does not override method from its superclass 错误。如何解决这个问题,以及如何实现 Firebase 登录。
谢谢。以下是我的代码:
public class DialogGoogle extends Dialog {
private Activity activity;
@BindView(R.id.btn_google)
LinearLayout btnGoogle;
private GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth mAuth;
private int RC_SIGN_IN = 1;
public DialogGoogle(Activity a, boolean isTab, int width, int height) {
super(a, R.style.DialogTheme);
this.activity = a;
this.isTab = isTab;
this.windowWidth = width;
this.windowHeight = height;
}
// 在 onCreate 方法中
// 初始化 Firebase Auth
mAuth = FirebaseAuth.getInstance();
// 配置 Google 登录
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(this.activity.getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this.activity, gso);
// 在创建了一些方法之后
public void signInGoogle() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// 在 startActivityForResult() 之后需要获取结果,我使用了 onActivityResult(),
// 但我无法在 DialogGoogle 类中重写这个方法。通常我会在按钮点击时运行 signInGoogle() 方法。
}
注意:这里仅为你提供了翻译后的代码部分,而不包括问题回答和额外的内容。如果你有其他翻译需求,请随时告诉我。
英文:
I have created Fragment and when click a button get popup like dialog alter view. in that view include google sign in button. when i connect to the firebase and continue the implementation after the startActivityForResult i need to override onActivityResult method. but popup corresponded class extend another Dialog class and can not override onActivityResult method. when going to override it shows method does not override method from its superclass error. how to solve this. and how to implement my firebase login.
thank you. here is my code
public class DialogGoogle extends Dialog { private Activity activity; @BindView(R.id.btn_google)
LinearLayout btnGoogle; private GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth mAuth;
private int RC_SIGN_IN = 1; public DialogGoogle(Activity a, boolean isTab, int width, int height) {
super(a, R.style.DialogTheme);
this.activity = a;
this.isTab = isTab;
this.windowWidth = width;
this.windowHeight = height;}
in onCreate method
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(this.activity.getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this.activity,gso);
After i created some method
public void signInGoogle() {Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);}
in after startActivityForResult() I need to get result and i used onActivityResult() but i cannot override method in that DialogGoogle class. i run usually signInGoogle() method in button click
答案1
得分: 0
你在Activity中启动Fragments,在那里重写onActivityResult方法,但首先要像文档中那样启动认证:
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.PhoneBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build(),
new AuthUI.IdpConfig.FacebookBuilder().build(),
new AuthUI.IdpConfig.TwitterBuilder().build());
// 创建并启动登录意图
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN);
RC_SIGN_IN是一个静态字段,这是一个指示,您可以检查Activity是否已完成的指示器。
英文:
You start Fragments in a Activity and there you Override the onActivityResult method but first you start your auth like in the documentation:
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.PhoneBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build(),
new AuthUI.IdpConfig.FacebookBuilder().build(),
new AuthUI.IdpConfig.TwitterBuilder().build());
// Create and launch sign-in intent
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN);
RC_SIGN_IN is a static field and this is the indicator that you can check if the Activity is finished.
专注分享java语言的经验与见解,让所有开发者获益!
评论