How to Configure Launcher Activity Programmatically in Android

How can I configure Launcher activity programmatically in android?

Long story short, you cannot change the Activity that is launched by default. Update: There is an alternative as described by CommonsWare in another answer.

However, there are reasonable work arounds. In your MainActivity you can check whether the user is logged in and immediately redirect them to the LoginActivity. That has the added benefit of automatically returning to the MainActivity after you have logged in.

Alternatively, you can always go first to the LoginActivity, and if the user is already logged in, send them to the MainActivity (rewrite the Intent history to remove the return to LoginActivity or set the noHistory flag in the manifest).

Programmatically change launcher activity

I would recommend having a helper activity that is always designated as the launcher activity in your manifest. Then, in the onCreate of that activity you can do whatever determination you need to decide what app to start and then finish the helper activity. Example:

In your manifest (launcher activity):

<activity android:name=".HelperActivity" ... />

Then, in HelperActivity's onCreate:

@Override
public void onCreate(Bundle b){
super.onCreate();
//determine what activity you want
startActivity(new Intent(this, NewActivity.class);
finish();
}

Change launch activity style programmatically

After some research I've discovered that this is not possible. Launch screens are used when we want to show something while our main functionality is loading, so we include the drawable we want to show in the Manifest to have this appearing fast while our main activity loads. This launch screen, as it is in the Manifest, appears before anything else, so if we could change the theme of the launch screen dinamically, we would lose that fast appearing while everything else loads.

How can I create a Launcher Activity programmatically?

As far as I know, it's not possible. Android creates or sets up the hard link of the App icons to their respective activities by looking at the manifest. If you don't set it, you will not find any icons/shortcuts for your app after you install it.

android: choose default launcher programmatically

Try using the following:

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

If a default action is already set (yours), you can call first:

getPackageManager().clearPackagePreferredActivities(getPackageName());

If the default action is not yours, you cannot clear it programmatically, what you can do is to check if other app is set as default and show a message..

private boolean isMyLauncherDefault() {
PackageManager localPackageManager = getPackageManager();
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
String str = localPackageManager.resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName;
return str.equals(getPackageName());
}

As a workaround in case of other app is set as default, you can created a fake home, install it (this will force the system to clear the default app) and then uninstall it...

Manifest.xml

<activity
android:name="FakeHome" android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

FakeHome.java

public class FakeHome extends Activity {

}

Somewhere

if (!isMyLauncherDefault()) {           
PackageManager p = getPackageManager();
ComponentName cN = new ComponentName(getApplicationContext(), FakeHome.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
startActivity(selector);

p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

How to change launcher activity in android when a certain condition is met?

I got the solution

I used handler to delay intent and startActivity, which helped me to change the activity as I needed



Related Topics



Leave a reply



Submit