开发者

How do i dynamically choose which activity to launch when opening an app

开发者 https://www.devze.com 2022-12-29 20:51 出处:网络
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

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 laun开发者_C百科ch the login activity, or the main app activity. Is there a way to do this?


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);


There is another way to do that using activity-alias.

  1. 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.


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); 

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消