简体   繁体   English

如何在非活动 class android 中调用 onActivityResult

[英]how to call onActivityResult in non Activity class android

I have created Fragment and when click a button get popup like dialog alter view.我已经创建了 Fragment,当单击按钮时会弹出对话框更改视图。 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.当我连接到 firebase 并在startActivityForResult之后继续执行时,我需要覆盖 onActivityResult 方法。 but popup corresponded class extend another Dialog class and can not override onActivityResult method.但弹出对应 class 扩展另一个对话框 class 并且不能覆盖 onActivityResult 方法。 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.以及如何实现我的 firebase 登录。

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在 onCreate 方法中

// 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.在 startActivityForResult() 之后我需要得到结果并且我使用了 onActivityResult() 但我无法覆盖 DialogGoogle class 中的方法。 i run usually signInGoogle() method in button click我通常在单击按钮时运行 signInGoogle() 方法

You start Fragments in a Activity and there you Override the onActivityResult method but first you start your auth like in the documentation:您在 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());
// 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. RC_SIGN_IN 是一个 static 字段,这是您可以检查活动是否完成的指示器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM