I'm making a blog app, the registration works great, when successful, a new intent where there's a userProfileActivity starts, I added in this activity a button to write a post (opens a new activity with editTexts to fill ), the problem is: the currentUser is null on all the activities except the registration one, does this have to do with calling the function finish() after opening a new activity? can you please explain to me how to keep the user logged in?
/*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);
}
}
Когда вы регистрируете нового пользователя в своем приложении, состояние всегда сохраняется (или должно быть)
Добавьте код, подтверждающий, что пользователь не вошел в систему.
@AlexMamo приложение вылетает, когда я нажимаю кнопку добавления публикации
@DougStevenson Извините, если мой код грязный, я новичок
Что такое сообщение об ошибке при сбое?
@AlexMamo, что текущий пользователь нулевой, это отображается в logcat, я использовал оператор if
@AlexMamo спасибо за указание еще раз проверить ошибку, после ответа «Eldho P James» сбой произошел из-за индикатора выполнения, я прокомментировал его findById (), спасибо




Чтобы получить текущий идентификатор пользователя в других ваших действиях, используйте этот код
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String userId =currentFirebaseUser.getUid();
Это пустая трата ресурсов на стороне сервера. Не думаю, что это правильное решение.
Измените свой вопрос, чтобы показать код, который доказывает, что пользователь не вошел в систему, когда вы думаете, что это должно быть.