当我站在firebase上时按下后退按钮验证UI mt应用程序不会退出而是显示带有hello world文本的活动

时间:2018-01-22 20:51:24

标签: android authentication firebase-authentication firebaseui

我正在使用firebase身份验证用户界面登录并点唱我的用户。当我的应用程序启动时,前面显示的第一个屏幕是firebase身份验证UI,当我站在Firebase身份验证UI上时按下后退按钮时,firebase身份验证UI消失,显示带有hello文本世界的空活动,然后应用程序退出再次按下后退按钮。当我站在Firebase身份验证用户界面时按下后退按钮时应用程序应该退出,因为这是我打开应用程序时用户可以看到的第一个屏幕。第一张图片是我打开应用程序时用户可以看到的图像,而第二张图片是我的用户在我的应用程序的第一个屏幕上按下后退按钮时可见的图像。This is the first dcreen which appers when user opens the app This screen becomes visible to user when he press the back button while standing on Firebase Authentication UI although app was suppose to exit if user presses back button while standing on Firebase Authentication UI because it is the first screen of app MainActivity.java

package com.example.anonymous.ghar_ka_khana;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.IdpResponse;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {


    private static final int RC_SIGN_IN = 123;
    private String signinthrough;
    private FirebaseAuth auth;
    private FirebaseAuth.AuthStateListener authStateListener;

    private FirebaseDatabase database;
    private DatabaseReference databaserefrence;
    private ChildEventListener databaselistener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //R.mipmap.ic_launcher;

        database = FirebaseDatabase.getInstance();
        databaserefrence= database.getReference().child("Users");


        auth = FirebaseAuth.getInstance();
        if (auth.getCurrentUser() != null)
        {
            Toast.makeText(getApplicationContext(), "Your toast message.", Toast.LENGTH_SHORT).show();
        }
        else
        {
            // not signed in
            startActivityForResult(
                    AuthUI.getInstance()
                            .createSignInIntentBuilder()
                            .setIsSmartLockEnabled(false)
                            .setAvailableProviders(
                                    Arrays.asList(
                                            new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build(),
                                            new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
                                            new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build()))
                            .setLogo(R.mipmap.ic_launcher)
                            .build(),
                    RC_SIGN_IN);
        }
    }

}

6 个答案:

答案 0 :(得分:0)

onBackPressed()存根中,编写以下代码

Intent homeIntent = new Intent(Intent.ACTION_MAIN);     
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(homeIntent);

或使用System.exit(0)

答案 1 :(得分:0)

阅读你的回答让我觉得你是个巨魔。这真的让我失去了对人性的希望。所以请尽量缩短你的句子。

答案 2 :(得分:0)

你应该覆盖onBackPressed(),按下后退按钮时会运行此方法。

答案 3 :(得分:0)

您能否展示一下如何在MainActivity中实现onActivityResult?

当Firebase用户界面登录屏幕可见时,由于SignInIntentBuilder,您的操作正在调用onActivityResult。您可以在onActivityResult中尝试以下内容。

IdpResponse response = IdpResponse.fromResultIntent(data);

if (resultCode == RESULT_OK) {
     //Signed In
} else {
//Check here if response is null or other ErrorCodes are received. take actions accordingly. 
    if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
            finish();
    }
}

答案 4 :(得分:0)

在您的(FirebaseUIActivity或MainActivity)类中的onActivityResult上进行操作,如果响应为null,这意味着用户按下了后退按钮,则可以使用finish()或createSignInIntent()

        if (resultCode == Activity.RESULT_OK) {
           //...
           //...
        }else{
            if (response == null) {
                // back button is pressed
                finish()
            }
        }

答案 5 :(得分:0)

这对我有用。在onActivityResult中:

    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
        if (resultCode == RESULT_OK) {
            // Successfully signed in
        }
        else {
            // Sign in failed. If response is null the user canceled the
            // sign-in flow using the back button. Otherwise check
            // response.getError().getErrorCode() and handle the error.
            // ...
            if (response==null){
                //The user has pressed the back button
                finish();
            }else{
                //Sign in failed due to some other reason
            }
        }
    }