Default activity in Android Studio problem

0

Hello, how are you? I have a question:

What happens is that I have my application made in Android Studio and I have three activities the Login menu and the submenu.

When I'm unlocked and close my application appears again in the login to log in and that is correct.

The problem is that when I leave the application and close it completely, this instead of starting in the main menu that I would like it to open, it always opens in the activity of the sub menu.

And my goal is to ensure that when the user logged in like this the application is closed, the main menu always appears

the serious question is there any way to put this main menu as default every time I open my application?

Loging is done with Firebase

This activity of the sub menu creates first before the main menu I think it may be that this sub menu is by default and I do not know if I can change it.

Please, if someone can help me with some idea.

    
asked by Julio Tobar 16.11.2018 в 08:58
source

2 answers

0

I would say that you put the following code in the onCreate function of your login.

In Kotlin.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    var mAuth = FirebaseAuth.getInstance().currentUser

    if(mAuth!=null){
        intent = Intent(this, EjemploActivity::class.java)
        startActivity(intent)
    }
}

In Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    FirebaseUser mAuth = FirebaseAuth.getInstance().getCurrentUser();
    if(mAuth!=null){
        startActivity(new Intent(getApplicationContext(),EjemploActivity.class));
    }
}
    
answered by 16.11.2018 в 15:46
0

What you have to use is a AuthStateListener , this will take care of listening if the mAuth.getCurrentUser () is different from null. If it is, we can send the user to another screen, since we would not need to log in again.

First we created the authListener

private FirebaseAuth.AuthStateListener mAuthListener;

Within the onCreate() declares this listener, this is done with the intention that each time the user passes through Login.class the listener will trigger and listen for changes in the auth, if the user is already logged in, we will direct it to the submenu screen

mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    startActivity(new Intent(Login.this,SubMenu.class);
                }
            }
        };

Then in the Override of onStart() we will attack this listener so that it listens for changes to our mAuth.

@Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener); 
    }

Finally onAuthStateChanged is going to be launched every time

  • After the Listener was added.
  • When the user is logged in.
  • When the user desloguea.
  • When the current user changes.
  • This is marked in this documentation.

        
    answered by 17.11.2018 в 23:30