0

I have an application that allows the user to use the apps without login (and with login too of course).

When the user hasn't logged in, I want my apps to show a fragment which asks the user to login (LoginFragment). And when the user already logged in, I want my apps to show the main fragment (HomeFragment).

I use firebase auth as my login "functionality". Here is my MainActivity code.

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

    mMainFrame = findViewById(R.id.main_frame);
    mMainNav = findViewById(R.id.main_nav);

    homeFragment = new HomeFragment();
    measureFragment = new MeasureFragment();
    accountFragment = new AccountFragment();
    loginFragment = new LoginFragment();

    setFragment(measureFragment);

    mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.nav_measure:
                    setFragment(measureFragment);
                    return true;
                case R.id.nav_home:
                    mAuth = FirebaseAuth.getInstance();
                    mAuthListener = new FirebaseAuth.AuthStateListener() {
                        @Override
                        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                            Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
                            user = firebaseAuth.getCurrentUser();
                            if (user != null) {
                                Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show();
                                setFragment(homeFragment);
                            } else {
                                Toast.makeText(MainActivity.this, "3", Toast.LENGTH_SHORT).show();
                                setFragment(loginFragment);
                            }
                        }
                    };
                    return true;
                case R.id.nav_account:
                    setFragment(accountFragment);
                    return true;
                    default:
                        return false;
            }
        }
    });
}

private void setFragment(Fragment fragment) {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame , fragment);
    fragmentTransaction.commit();
}

Here is my mockup

  1. if the user haven't log in User haven't log in, the "LoginFragment" is showed

  2. if the user already login User already login, the "HomeFragment" is showed

  • Check [this](https://stackoverflow.com/a/50705537/7671879) type of approach. – miguelarc Aug 02 '18 at 08:52
  • I already use that kind of approach, but my code is not working. I need a code solution – Giovanka Bisano Aug 02 '18 at 09:07
  • What is not working? By what i can see in your code, you are replacing the previous fragment, and by this your back press won't make you return to the previous screen/fragment. – miguelarc Aug 03 '18 at 11:48

0 Answers0