[英]Using FirebaseAuthListener and it will navigate to activity with fragment if there is no user logged in
What I am trying to do here is when I launch my MainActivity
with FirebaseAuthListener
and there is no user logged in it will navigate to the MainFragment and the MainFragment
is for the container that connects to my SignInFragment
(this SignInFragment
is extended Fragment). 我要在这里执行的操作是,当我使用
FirebaseAuthListener
启动MainActivity
,没有用户登录,它将导航到MainFragment,而MainFragment
是连接到我的SignInFragment
的容器(此SignInFragment
是扩展的Fragment)。 So basically, the MainFragment
is extended to AppCompatActivity
. 因此,基本上,
MainFragment
被扩展为AppCompatActivity
。
It actually works all but my problem is the MainFragemnt
is blank in my emulator, it supposed to be display the LoginFragment
. 它实际上可以正常工作,但是我的问题是模拟器中的
MainFragemnt
是空白的,应该显示LoginFragment
。
Here are my codes: MainActivity
class 这是我的代码:
MainActivity
类
//Firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private TextView mWelcome;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWelcome = findViewById(R.id.tvWelcome);
setupFirebaseAuth();
}
/*
--------------------------------------Firebase-----------------------------------------
*/
private void getCurrentUser(FirebaseUser user){
Log.d(TAG, "getCurrentUser: checking if user is logged in.");
if (user == null){
Intent intent = new Intent(this, MainFragment.class);
startActivity(intent);
}
}
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
//Check if the user is logged in
FirebaseUser user = firebaseAuth.getCurrentUser();
getCurrentUser(user);
if (user != null){
//User is signed in
Log.d(TAG, "onAuthStateChanged: signed _in: " + user.getUid());
} else {
//User is signed out
Log.d(TAG, "onAuthStateChanged: signed_out");
}
}
};
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onStop() {
super.onStop();
if (mAuthListener != null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
My codes for MainFragment
: 我的
MainFragment
代码:
public class MainFragment extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.fragment_main);
SignInFragment fragment = new SignInFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment, null).commit();
My code for SignInFragment
: 我的
SignInFragment
代码:
public class SignInFragment extends Fragment {
private static final String TAG = "SignInFragment";
private TextView mEmail, mPassword, mForRegister;
private EditText eEmail, ePassword;
private Button btnLogin;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.signin_fragment,container,false);
//TextView
mEmail = view.findViewById(R.id.tvEmail);
mPassword = view.findViewById(R.id.tvPassword);
mForRegister = view.findViewById(R.id.tvForRegister);
//EditText
eEmail = view.findViewById(R.id.etEmail);
ePassword = view.findViewById(R.id.etPassword);
//Button
btnLogin = view.findViewById(R.id.btnLogin);
//Navigating to RegisterFragment
mForRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.container, new RegisterFragment());
fr.commit();
}
});
return view;
}
This is the picture of the emulator: 这是模拟器的图片:
try changing your onCreate method. 尝试更改您的onCreate方法。 use the following instead:
请改用以下内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.