1

I am developing an android application and want to launch as launcher at run time without using category tag in manifest file.

<activity
        android:name="com.sample.test.MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
  • you can refer here http://stackoverflow.com/questions/7414272/how-to-launch-an-android-app-without-android-intent-category-launcher – Raju Nov 11 '16 at 06:42

1 Answers1

1

there is no way to do it directly the way you want. To set a launcher activity, you have to edit the manifest, and you can't do it actually.
But what you can do is to declare some LauncherActivity in a manifest using this intent filter:

<activity
        android:name="com.sample.test.LauncherActivity"
        android:label="@string/title_activity_main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

And inside of it's onCreate() method read previously saved activity name, you really need to start by clicking on app icon from SharedPreferences, and start it. Like this:

public class LauncherActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String activityToLaunch = getSharedPreferences("LauncherActivity", MODE_PRIVATE).getString("LauncherActivity", "Some default Activity");
        Intent intent;
        switch (activityToLaunch) {
            case "SomeActivity1":
                intent = new Intent(this, SomeActivity1.class);
                break;
            case "SomeActivity2":
                intent = new Intent(this, SomeActivity2.class);
                break;
            case "SomeActivity3":
                intent = new Intent(this, SomeActivity3.class);
                break;
            default:
                intent = new Intent(this, SomeDefaultActivity.class);
                break;
        }
        startActivity(intent);
        finish();
    }
}
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52