How can I dynamically choose which activity to launch when the application opens?

I am writing an application that requires you to be logged in before use. From my understanding of android so far you need to choose which activity to launch when you open from the launch in the manifest. I do not know what activity I want to run at compile time. I want the user to click the icon, then I check and check if they are logged in, and then decide based on this whether to start the login operation or the main action of the application. Is there any way to do this?

+53
android android-manifest
May 05 '10 at 19:19
source share
4 answers

No, since you need to run some kind of code, there is no way to declaratively (in the manifest) say this. You need to start the action (set in the manifest), and then take this action based on whether the user is logged in or not, which second activity to launch through Intent:

final Class<? extends Activity> activityClass; if(userIsLoggedOn()) activityClass = LoggedOnActivity.class; else activityClass = LogInActivity.class; Intent newActivity = new Intent(context, activityClass); context.startActivity(newActivity); 
+64
May 05 '10 at 19:29
source share

There is another way to do this 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. In other places of entry into the system:

     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 application will be killed once and the next time the application starts, MainActivity will be the launch.

+6
Apr 16 '16 at 20:28
source share

Android Framewowrk provides a method
public Intent setClassName (String packageName, String className)

an Intent class, which you can use to dynamically choose which action to invoke simply by using the class name in String.

Here is an example

  String packageName = getPackageName(), className=packageName+"subFolder.myActivity"; Intent i = new Intent(); i.setClassName(packageName, className); startActivity(i); 

https://developer.android.com/reference/android/content/Intent.html#setClassName(java.lang.String,%20java.lang.String)

+1
Jun 05 '19 at 6:30
source share

As above, @auval said I am testing the code as shown below and it succeeds! First, the AndroidManifest.xml file looks 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> 

Secondly, you can put this code somewhere in 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 started the application, LauncherActivity will be launched, and when you exit the application, start the application again, MainActivity will be launched.

0
Jul 29 '17 at 9:53 on
source share



All Articles