Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 455
msfeldstein Avatar asked May 05 '10 19:05

msfeldstein


People also ask

How does Android know which activity to run first?

Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context. startActivity(). So, CATEGORY_DEFAULT can appear number of times. Android does not grab whichever one appears first in the manifest but it starts with activity having CATEGORY_LAUNCHER.

How do you start an activity only once the app is opened for the first time?

It is important to check that the first activity which opens when the app is launched is MainActivity. java (The activity which we want to appear only once). For this, open the AndroidManifest. xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once.

How do I use intent to launch a new activity?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);


2 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); 
like image 131
Ricardo Villamil Avatar answered Oct 12 '22 00:10

Ricardo Villamil


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.

like image 32
Ali.DM Avatar answered Oct 12 '22 01:10

Ali.DM