How can I configure Launcher activity programmatically in android?

后端 未结 6 793
忘掉有多难
忘掉有多难 2020-11-30 09:56

I am working on an app with two activities : LoginActivity and MainActivity. When the user first opens the app he will login and his credentials (u

6条回答
  •  没有蜡笔的小新
    2020-11-30 10:45

    As far as I know changing launcher programmatically is not possible, but it also doesn't make sense.

    On your LoginActivity's onCreate check if a username and token is already saved, if it is try to login with that automatically, is succeed redirect to your MainAcivity. Depending on the way your app works you can have a variable that checks if a user is logged in or not, if he is the LoginActivity would redirect him to MainActivity without trying to log in again.

    //LoginActivity
    onCreate(Bundle bundle)
    {
        /* ... */
    
        //Or whatever you use to login (it could also go inside a thread or an AsyncTask
        if (login())
        {
            //Intent
            Intent intent = new Intent(this, MainActivity.class);
    
            //Start Activity
            startActivity(intent);
    
            //Finish this activity, so when user pressed back the login activity will not come forth and  the app will exit 
            //this looks like when a user has logged in once, the login screen will not be visible to him (unless you want to)
            finish();
        }
    }
    

    You can also configure it to save username and token only if a login is successful which means the above code can be modified like this:

    if (getUsername() != null)
    {
        /* Start Main Activity */
    }
    

    This won't attempt to log in, but it knows the credential are right since it has logged in at least once with them.

    If your app behaves a different way that these methods do not work, feel free to say so, I may be able to provide more info

提交回复
热议问题