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;
}
}
In Android, you can configure the starting activity (default activity) of your application via following “intent-filter” in “AndroidManifest. xml“. See following code snippet to configure a activity class “logoActivity” as the default activity.
To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example: <manifest ... > The only required attribute for this element is android:name, which specifies the class name of the activity.
The Android system finds the default activity to launch by looking at your AndroidManifest. xml file. When you add a new activity, then that activity will be added to the manifest file as well, but without the <intent-filter> tag.
You want to open the file called AndroidManifest. xml. The default Activity name is the class named [package name/bundle id].
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With