Firebase CurrentUser Null,如何让用户登录所有活动?

时间:2018-04-02 14:29:17

标签: java android firebase firebase-authentication

我正在制作一个博客应用,注册效果很好,一旦成功,一个新的意图,其中有一个userProfileActivity开始,我在这个活动中添加了一个按钮来写一个帖子(打开一个新的活动与editTexts填充),问题是:currentUser在除注册之外的所有活动上都为null,这是否与打开新活动后调用函数finish()有关? 你能告诉我如何让用户登录吗? 的

/*after successful registration,the profile activity starts, here's what inside the oncreate methode*/

fabAddClasseP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent addPostIntent = new Intent(ProfileActivity.this, addPostActivity.class);

                startActivity(addPostIntent);


        }
    });

的 的

/* the addPostActivity*/
/* declaration*/
    private FirebaseAuth auth;
/*a reference to the post*/
    private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("dataPost");
/*a refernce to the user/author of post*/
    private DatabaseReference mDatabaseUser = FirebaseDatabase.getInstance().getReference().child("dataUser");

//then below on the onCreate methode, i make sure all edit texts are filled, 

buttonSavePost.setOnClickListener(new View.OnClickListener() {
//if  all filds are not empty
String PostId = mDatabase.child("PostId").push().getKey();



 //getting the current user id if not null
String userId = auth.getCurrentUser().getUid();





                       writeNewDataPost( userId, PostId, titlePost, contentPost);


    private void writeNewDataPost(String user_id,String post_id, String title, String content) {

        DataPost dataP = new DataPost(title,  content);

            mDatabase.child(Post_id).setValue(dataP);
/*adding a 

node to the current post "author, and setting its value to the current logged in user*/    
    mDatabase.child(post_id).child("author").setValue(user_id);

               }
        }

1 个答案:

答案 0 :(得分:1)

要将当前用户ID引入其他活动,请使用此代码

FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

String userId =currentFirebaseUser.getUid();