8

My application is composed by a few activity.

Activity A is my main menu with some icons. This Activity can launch depending on which icon you press: Activity B,C,D,E or F.

That's fine and really easy, Activity A is the default one.

Now, I made an option in preference that allow users to start their favourite activity.

Some users will in fact prefer to get directly Activity B for instance.

The only way I found a solution was to do this in Activity A This solution is very ugly because Activity A will always launch and close automatically:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    final Intent intent = getIntent();
    String action = intent.getAction();

    if (Intent.ACTION_MAIN.equals(action)) {
        switch (Integer.valueOf(settings.getString("Activitypref", "1"))) {
        case 2:
            Intent i = new Intent(ActivityA.this, ActivityB.class);
            finish();
            startActivity(i);
            break;
        case 3:
            i = new Intent(ActivityA.this, ActivityC.class);
            finish();
            startActivity(i);
            break;
        case 4:
            i = new Intent(ActivityA.this, ActivityD.class);
            finish();
            startActivity(i);
            break;
        case 5:
            i = new Intent(ActivityA.this, ActivityE.class);
            finish();
            startActivity(i);
            break;
        case 6:
            i = new Intent(ActivityA.this, ActivityF.class);
            finish();
            startActivity(i);
            break;
        default:
            break;
        }
    } 
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • 1
    If Activity A's sole purpose is to decide which other activity to launch, then I don't see this as an ugly solution. It sounds like Activity A is doing its job. – DeeV Jun 27 '11 at 19:16
  • Yes, but user can choose to "bypass" this activity and come directly to Activity X. When set into preferences, I don't want Activity A to be launched except when user press my Home button, after having enjoyed Activity X. – Waza_Be Jun 27 '11 at 19:18
  • The most annoying stuff is the loading time and the screen blinking whan starting Activity A and closing it into the "onCreate" – Waza_Be Jun 27 '11 at 19:23
  • 1
    You can create a class that extends "Application" and have it decide which activity to launch. In the "Application Attributes" section of the "Application" tab of the manifest, you have to assign "Name" to the name of the class that extends it. Since it's not supposed to handle UI, it sounds like a good place for the switch statement. Remove the "finish()" calls though. – DeeV Jun 27 '11 at 19:30

1 Answers1

9

Instead of ActivityA, consider using wrapper activity to be called from launcher. You will eliminate a need for checking for ACTION_MAIN. Also you can store target activity name in preferences and use it to directly start your target activity via different intent signature:

public Intent (String action)

 <activity class=".StartActivity" android:label="...">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
 </activity>

 <activity class=".ActivityA" android:label="...">
             <intent-filter>
                 <action android:name="mypackage.ActivityA" />                    
             </intent-filter>
 </activity>

And in StartActivity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());

    String action = settings.getString("Activitypref","mypackage.ActivityA");
    Intent intent = new Intent(action);
    startActivity(intent);
    ....
}

You may need to fiddle around little bit to get it right.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • Hello ALex, your solution seem exactly what I am looking for, but unfortunately, I don't understand what you mean.. I feel a little bit stupid, but do you a small snippet or example? – Waza_Be Jun 27 '11 at 19:27
  • I added an update. I don't have access to my Android code right now, so you may need to fiddle with it to get it right. – Alex Gitelman Jun 27 '11 at 19:35
  • 1
    Answer is great but your wripper is like my previous ACtivity A and make the screen blink a little bit ( StartActivity is launched then ACtivityA) then, I also need to finish it, that's quite the same then before, in fact.. – Waza_Be Jun 27 '11 at 19:54
  • Could you elaborate please, what value exactly should I pass to `startActivity()` method? I'm trying to run one of pre-defined activities based on some condition. – Slava Fomin II Mar 06 '15 at 11:51