How Do I Dynamically Choose Which Activity To Launch When Opening An App
Solution 1:
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);
Solution 2:
There is another way to do that using activity-alias.
In the Manifest :
<activityandroid:name=".LoginActivity"android:icon="@drawable/ic_launcher_main"android:label="Login" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".MainActivity"android:icon="@drawable/ic_launcher_main"android:label="MainActivity" ></activity><activity-aliasandroid:name=".AliasActivity"android:label="AliasActivity"android:enabled="false"android:targetActivity=".MainActivity" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity-alias>
2.Somewhere in the Login Activity:
Strings= getApplicationContext().getPackageName(); ComponentNamecm=newComponentName(s, s+".AliasActivity"); ComponentNamecm2=newComponentName(s, s+".Login"); PackageManagerpm=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.
Solution 3:
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
StringpackageName= getPackageName(), className=packageName+"subFolder.myActivity";
Intenti=newIntent();
i.setClassName(packageName, className);
startActivity(i);
Solution 4:
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:
<activityandroid:name=".LauncherActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity><activityandroid:name=".MainActivity"></activity><activity-aliasandroid:name=".AliasActivity"android:enabled="false"android:targetActivity=".MainActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity-alias>
Second,you can put these code in somewhere in the MainActivity.class:
privatevoidchangeLauncher() {
Strings= getApplicationContext().getPackageName();
ComponentNamecm=newComponentName(s, s + ".AliasActivity");
ComponentNamecm2=newComponentName(s, s + ".LauncherActivity");
PackageManagerpm=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.
Post a Comment for "How Do I Dynamically Choose Which Activity To Launch When Opening An App"