I am writing an app that requires you to be logged in to a service before using it. From my understanding of android so far, you have to choose which activity to launch when you open from the launcher in the manifest. I don't know which activity i want to launch at compile time. I want the user to click the icon, then I check and see if they're logged in, then decide based on that whether to launch the login activity, or the main app activity. Is there a way to do this?
4 Answers
No, since you have to run some code, there's no way to declaratively (in manifest) to say this. You have to launch an activity (set in manifest), then have this activity decide based on if the user is logged on or not what second activity to launch via Intent:
final Class<? extends Activity> activityClass;
if(userIsLoggedOn())
activityClass = LoggedOnActivity.class;
else
activityClass = LogInActivity.class;
Intent newActivity = new Intent(context, activityClass);
context.startActivity(newActivity);

- 5,031
- 2
- 30
- 26
-
58One thing to note is that it is possible to launch an activity without displaying a UI, so you can get it to look like it is doing what you want... Note that your logic should be done fairly quickly so it isn't noticable to the user. Just put the following code inside your activity's manifest: android:theme="@android:style/Theme.NoDisplay" – Justin Feb 03 '11 at 22:52
-
Can you detect that you are going to Main on launch vs. some other time? Since one might want to goto a different activity based on whether it is a launch? – Androider Feb 19 '11 at 23:26
-
3Does this mess with the hierarchy? i.e. can a user press the back button and end up at this activity? – James Parsons Oct 01 '15 at 01:31
-
@James_Parsons just make sure you don't include a PARENT_ACTIVITY in the activity's meta-data. – Malfunction Jan 29 '16 at 14:10
-
1Häh, what's about the Application class? If you implement a subclass of android.app.Application you should run code without displaying an Activity or am I wrong?! – Ichor de Dionysos Feb 19 '16 at 19:46
-
@IchordeDionysos Yes, but you should run the minimum code possible on that Application as it will affect the launch times of the app. – Joaquin Iurchuk Apr 13 '16 at 14:52
There is another way to do that using activity-alias.
In the Manifest :
<activity android:name=".LoginActivity" android:icon="@drawable/ic_launcher_main" android:label="Login" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:icon="@drawable/ic_launcher_main" android:label="MainActivity" > </activity> <activity-alias android:name=".AliasActivity" android:label="AliasActivity" android:enabled="false" android:targetActivity=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
2.Somewhere in the Login Activity:
String s = getApplicationContext().getPackageName(); ComponentName cm = new ComponentName(s, s+".AliasActivity"); ComponentName cm2 = new ComponentName(s, s+".Login"); PackageManager pm = this.getPackageManager(); pm.setComponentEnabledSetting(cm, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 1); pm.setComponentEnabledSetting(cm2, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
after that, the app will be killed once and next time you launch app, the MainActivity would be the launcher.
-
1Cool!! The only solution that doesn't require extra activity and reading shared preferences on the UI thread. You can use the flag PackageManager.DONT_KILL_APP instead of 0 or 1, so the app won't exit. There is still one issue that prevents me from using this: next run from Android Studio fails. (activity is disabled) – Amir Uval Jan 07 '17 at 16:02
-
I worked around this limitation by adding a run configuration for the main activity. So this solution works well – Amir Uval Jan 07 '17 at 16:34
-
-
This unfortunately breaks the icons in some launchers and deletes icon shorcuts added to home screen on most. – Igor Čordaš Aug 10 '18 at 15:18
-
As Igor said, seem good but not working stable. In some case, launcher icon doesn't work. When i changed activity and quit application, I sometimes saw "Application is not installed" toast. After one second, the launcher activity was changed. – ilkayaktas Dec 02 '20 at 12:36
The Android Framewowrk provides the method
public Intent setClassName (String packageName, String className)
of the Intent class that you can use to dynamically choose which activity to invoke with just the name of the class in String.
Here's an example
String packageName = getPackageName(), className=packageName+"subFolder.myActivity";
Intent i = new Intent();
i.setClassName(packageName, className);
startActivity(i);

- 1,568
- 2
- 19
- 22
Just as above @auval said, I test the code as below and it do well! At first ,the AndroidManifest.xml file is look like this:
<activity
android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity">
</activity>
<activity-alias
android:name=".AliasActivity"
android:enabled="false"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>
Second,you can put these code in somewhere in the MainActivity.class:
private void changeLauncher() {
String s = getApplicationContext().getPackageName();
ComponentName cm = new ComponentName(s, s + ".AliasActivity");
ComponentName cm2 = new ComponentName(s, s + ".LauncherActivity");
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(cm,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP );
pm.setComponentEnabledSetting(cm2,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
Now,when you first launch the app ,the LauncherActivity would be launched and when you exit the app ,run the app again,the MainActivity would be launched.

- 21
- 4