How can I configure Launcher activity programmatically in android?
Asked Answered
R

6

24

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 (username and token) are saved in Preferences.

Now, if the user opens the app again then MainActivity should start. I tried to switch between these activities in Application class and removed intent-filter for LAUNCHER_ACTIVITY from manifest, but it doesn't work.

Is there any way of switching between Launcher Activities programmatically on basis of saved preferences?

Rosenblatt answered 8/4, 2015 at 14:17 Comment(3)
check if logged in at LoginActivity using Preferences, then open MainAcivity and finish LoginActivity if user is logged inVictoria
If I check preferences in MainActivity then it will be better, as switching to login activity will occur less no of times, right ?Rosenblatt
could be, it depends on your business, if some data is fetched once mainAcitivty is loaded, then you might get some errors if user is not logged in, otherwise it could be fine.Victoria
R
19

Long story short, you cannot change the Activity that is launched by default. Update: There is an alternative as described by CommonsWare in another answer.

However, there are reasonable work arounds. In your MainActivity you can check whether the user is logged in and immediately redirect them to the LoginActivity. That has the added benefit of automatically returning to the MainActivity after you have logged in.

Alternatively, you can always go first to the LoginActivity, and if the user is already logged in, send them to the MainActivity (rewrite the Intent history to remove the return to LoginActivity or set the noHistory flag in the manifest).

Rotor answered 8/4, 2015 at 14:21 Comment(0)
T
28

Is there any way of switching between Launcher Activities programmatically on basis of saved preferences ?

You can try this:

Step #1: Have LoginActivity have the LAUNCHER <intent-filter> as normal, and have MainActivity have no <intent-filter>.

Step #2: Have an <activity-alias> element in the manifest pointing to MainActivity that has the LAUNCHER <intent-filter>.

Step #3: Put android:enabled="false" on the <activity-alias>, so it is disabled by default, so when the app is first installed, the only launcher icon is for LoginActivity.

Step #4: When the user logs in, and you want to change so MainActivity is the launcher activity, use PackageManager and setComponentEnabledSetting() to make the <activity-alias> enabled and to disable the LoginActivity.

Not all home screens will detect this change on the fly, and for those, the device would need to reboot in all likelihood to pick up the change. For this reason, it would be better to stick with a single launcher activity. If you want, that launcher activity could have Theme.NoDisplay and simply route to the correct "real" activity in onCreate(), per Fahim's answer.

Tripping answered 8/4, 2015 at 14:35 Comment(0)
R
19

Long story short, you cannot change the Activity that is launched by default. Update: There is an alternative as described by CommonsWare in another answer.

However, there are reasonable work arounds. In your MainActivity you can check whether the user is logged in and immediately redirect them to the LoginActivity. That has the added benefit of automatically returning to the MainActivity after you have logged in.

Alternatively, you can always go first to the LoginActivity, and if the user is already logged in, send them to the MainActivity (rewrite the Intent history to remove the return to LoginActivity or set the noHistory flag in the manifest).

Rotor answered 8/4, 2015 at 14:21 Comment(0)
A
4

An activity doesn't necessarily require a UI, so you can use the launcher activity in the manifest to lauch any activity you desire.

Across answered 8/4, 2015 at 14:21 Comment(0)
H
4

The easiest way is to make MainActivity launcher activity, as usual.
Then check in MainActivity#onCreate(Bundle) via SharedPreferences if the user already logged in and, if not, start LoginActivity immediately. When user logs in, save the boolean flag indicating that user logged in in SharedPreferences and finish MainActivity.

Hackathorn answered 8/4, 2015 at 14:22 Comment(0)
S
3

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

Showbread answered 8/4, 2015 at 14:17 Comment(0)
S
0

You can jsut add Intent after OnCreate to the XML you want to show in the beginning of your APP.

public class LoginActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       //add some code to detect if user is logged in
       if (user != null){
           Intent in = new Intent(LoginActivity.this, YourDesiredActivity.class);
           startActivity(in);
       } 
   ..........
   ...........
Sartorial answered 22/9, 2016 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.