Firebase,数字身份验证错误,活动被破坏问题

Posted

技术标签:

【中文标题】Firebase,数字身份验证错误,活动被破坏问题【英文标题】:Firebase, Digits Authentication Error, Activity Destroyed issue 【发布时间】:2017-06-05 07:30:32 【问题描述】:

我正在尝试通过 Firebase 登录并遇到一个奇怪的问题。除了一些三星设备,我可以在所有设备上成功登录。调试时我发现问题:

package com.apponative.committeeapp.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.apponative.committeeapp.R;
import com.apponative.committeeapp.datamodles.User;
import com.apponative.committeeapp.firebase.FireBaseDbHandler;
import com.apponative.committeeapp.utils.CommitteeCallBack;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.digits.sdk.android.AuthCallback;
import com.digits.sdk.android.AuthConfig;
import com.digits.sdk.android.Digits;
import com.digits.sdk.android.DigitsException;
import com.digits.sdk.android.DigitsSession;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class SplashActivity extends Activity
        implements CommitteeCallBack.FireBaseAuthCallBack, GoogleApiClient.OnConnectionFailedListener, AuthCallback 


    private int screenTag;
    private User user;
    private ImageView progressBar;

    // Firebase

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    AuthConfig.Builder builder = new AuthConfig.Builder();
    AuthConfig authConfig;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);

        fireBaseDigitsAuth();
    


    void fireBaseDigitsAuth() 

        mAuthListener = new FirebaseAuth.AuthStateListener() 
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) 

                if (firebaseAuth.getCurrentUser() != null) 

                
            
        ;

        mAuth = FirebaseAuth.getInstance();
        builder.withAuthCallBack(this);
        authConfig = builder.build();

        if (mAuth.getCurrentUser() != null) 
            FireBaseDbHandler.getDbHandler(this).userProfileCheck(mAuth.getCurrentUser().getEmail().split("@")[0]);
         else 
            Digits.authenticate(authConfig);
        
    

    public void signUpNewUser(final String email, String password) 
        mAuth.createUserWithEmailAndPassword(email + getString(R.string.firebase_domain), password)
                .addOnCompleteListener(SplashActivity.this, new OnCompleteListener<AuthResult>() 
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) 
                        if (task.isSuccessful()) 
                            FireBaseDbHandler.getDbHandler(SplashActivity.this).userProfileCheck(email);
                        
                    
                );
    

    public void signInExistingUser(final String email, final String password) 
        mAuth.signInWithEmailAndPassword(email + getString(R.string.firebase_domain), password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() 
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) 
                        if (!task.isSuccessful()) 
                            signUpNewUser(email, password);
                         else 
                            FireBaseDbHandler.getDbHandler(SplashActivity.this).userProfileCheck(email);
                        
                    
                );
    

    void startMainActivity(int itemId, User user) 
//        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        if (user != null) 
//            intent.putExtra("userId", user.getUserId());
//            intent.putExtra("userName", user.getUsername());
//        
//        intent.putExtra("itemId", itemId);
//        startActivity(intent);
    

    @Override
    public void onStart() 
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    

    @Override
    public void onStop() 
        super.onStop();
        if (mAuthListener != null) 
            mAuth.removeAuthStateListener(mAuthListener);
        
    

    @Override
    public void onBackPressed() 
        super.onBackPressed();
      //  finish();
    

    @Override
    public void SignInSuccess(User user) 
        progressBar.setVisibility(View.GONE);
        this.user = user;
        this.screenTag = R.string.tag_home;
         startMainActivity(screenTag, user);
    

    @Override
    public void NewUserRegisteration() 
        progressBar.setVisibility(View.GONE);
        this.user = null;
        this.screenTag = R.string.tag_setprofile;
        startMainActivity(screenTag, user);
    

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) 

    


    @Override
    public void success(DigitsSession session, String phoneNumber) 
        signInExistingUser(phoneNumber, phoneNumber);
    

    @Override
    public void failure(DigitsException error) 
        error.printStackTrace();
    

报错如下:

Process: com.apponative.committeeapp, PID: 28641
                                                                             java.lang.IllegalStateException: Activity has been destroyed
                                                                                 at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1455)
                                                                                 at android.app.BackStackRecord.commitInternal(BackStackRecord.java:687)
                                                                                 at android.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:667)
                                                                                 at com.google.android.gms.internal.zzaay.zzt(Unknown Source)
                                                                                 at com.google.android.gms.internal.zzaaw.zzc(Unknown Source)
                                                                                 at com.google.android.gms.internal.zzaaw.zzs(Unknown Source)
                                                                                 at com.google.android.gms.tasks.zzh$zza.zzw(Unknown Source)
                                                                                 at com.google.android.gms.tasks.zzh.addOnCompleteListener(Unknown Source)
                                                                                 at com.apponative.committeeapp.ui.SplashActivity.signInExistingUser(SplashActivity.java:115)
                                                                                 at com.apponative.committeeapp.ui.SplashActivity.success(SplashActivity.java:209)
                                                                                 at com.digits.sdk.android.WeakAuthCallback.success(WeakAuthCallback.java:32)
                                                                                 at com.digits.sdk.android.LoginResultReceiver.onReceiveResult(LoginResultReceiver.java:64)
                                                                                 at android.os.ResultReceiver$MyResultReceiver.send(ResultReceiver.java:59)
                                                                                 at android.os.ResultReceiver.send(ResultReceiver.java:93)
                                                                                 at com.digits.sdk.android.DigitsControllerImpl$1.run(DigitsControllerImpl.java:178)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我没有切换任何片段,这是一个简单的活动。我在登录前通过 Twitter Digits sdk 确认手机号码。它工作正常。在通过 Digits 确认后,我只会崩溃一次。重新启动不发送数字确认的应用程序运行顺利。

我想知道 Digits 是否在这方面遇到了麻烦。在调试时,我发现 Twitter Digits startPhoneNumberActivity 带有标志Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP。我认为这些标志正在破坏以前的活动。

我已经尝试过这个活动,但没有用:

android:persistent="true"
android:noHistory="false"

Build.gradle:

buildscript 
    repositories 
        maven  url 'https://maven.fabric.io/public' 
    

    dependencies 
        classpath 'io.fabric.tools:gradle:1.+'
    

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories 
    maven  url 'https://maven.fabric.io/public' 
    maven  url "https://jitpack.io" 



android 
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig 
        applicationId "com.---------"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        configurations.all 
            resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
        

        dexOptions 
            javaMaxHeapSize "4g"
        
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    
    compileOptions 
        targetCompatibility JavaVersion.VERSION_1_7
        sourceCompatibility JavaVersion.VERSION_1_7
    


dependencies 
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
        exclude group: 'com.android.support', module: 'support-annotations'
    )
    compile('com.digits.sdk.android:digits:2.0.6@aar') 
        transitive = true;
    
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.google.firebase:firebase-auth:10.0.1'
    compile 'com.google.firebase:firebase-database:10.0.1'
    compile 'com.android.support:design:25.3.1'

    compile 'com.google.firebase:firebase-storage:10.0.1'

    compile 'com.firebaseui:firebase-ui-storage:1.1.1'
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    testCompile 'junit:junit:4.12'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') 
        transitive = true;
    
    compile 'com.android.support:multidex:1.0.1'



apply plugin: 'com.google.gms.google-services'

【问题讨论】:

发布你的完整活动 @AvinashRoy 在调试时我发现 Twitter Digits startPhoneNumber 活动带有标志 'Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP' 。我认为这些标志正在破坏以前的活动。 添加你的 build.gradle 文件@SairaNawaz sry build.gradle 文件 检查可能对您有帮助的解决方法 【参考方案1】:

FLAG_ACTIVITY_CLEAR_TOP 是杀死活动的问题。

调用 Fabric.with(this, new TwitterCore(authConfig), new Digits());在 onCreate() 活动方法中可以为您解决问题。

【讨论】:

我无法删除它,因为它在 Twiiter Digits 类中。 Digits 通过 builder 发起活动。 intent.set flags ,你不能在这里删除它吗?? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 它是 SDK 的类,该类中的 Activity 正在使用私有方法启动,并且该类是“锁定”不可编辑不可扩展。 不,数字不提供访问权限

以上是关于Firebase,数字身份验证错误,活动被破坏问题的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 身份验证,错误:NoSuchMethodError:在 null 上调用了方法“登录”

React/Rebase/Firebase 警告:权限被拒绝 - 如何添加用户身份验证?

如何在视图模型中实现 Firebase 电话身份验证?

Firebase 电话身份验证失败

在Firebase身份验证后启动MainActivity

使用 Firebase 身份验证和谷歌登录时注销时“GoogleApiClient 尚未连接”