我正在制作一个博客应用程序,注册效果很好,在成功时,在有userprofileactitive开始的新意图时,我在此活动中添加了一个按钮来编写帖子(打开A填充的新活动(是:当前使用者对除注册外的所有活动中都是无效的,这是否与打开新活动后调用函数饰面((有关吗?您能否向我解释如何保持用户登录?
/*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);
}
}
将当前的用户ID进入其他活动使用此代码
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String userId =currentFirebaseUser.getUid();