Skip to content

Conversation

@dellis1972
Copy link
Contributor

@dellis1972 dellis1972 commented Nov 26, 2024

Context https://developer.android.com/guide/topics/manifest/activity-alias-element.
Fixes https://dev.azure.com/devdiv/DevDiv/_workitems/edit/2305723.

So activity-alias allows users to change some settings before calling an activity.
The idea is that your activity is no longer the main launcher, you still have that entry, but the activity-alias' become the main launchers.
Here is a sample AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
	<application
		android:allowBackup="true"
		android:icon="@mipmap/appicon"
		android:roundIcon="@mipmap/appicon_round"
		android:supportsRtl="true">
		<activity-alias
			android:name=".MainActivityAlias"
			android:enabled="true"
			android:icon="@mipmap/appicon"
			android:targetActivity=".MainActivity"
			android:exported="true">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity-alias>
		<activity-alias
			android:name=".MainActivityAlias2"
			android:enabled="false"
			android:icon="@mipmap/appicon2"
			android:targetActivity=".MainActivity"
			android:exported="true">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity-alias>
	</application>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
</manifest>

The MainActivity.cs then becomes

[Activity(Theme = "@style/Maui.SplashTheme", LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    [Register("com.companyname.mauiappwithmultipleicons.MainActivity")]
    public class MainActivity : MauiAppCompatActivity

Note that we do not use the MainLauncher property on the ActivityAttribute.

Copy link
Member

@pjcollins pjcollins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, but can we add a test? I think we may already have a good spot to add to https://github.com/dotnet/android-tools/blob/main/tests/Xamarin.Android.Tools.AndroidSdk-Tests/AndroidAppManifestTests.cs#L49

@dellis1972 dellis1972 requested a review from pjcollins December 3, 2024 10:39
@dellis1972 dellis1972 merged commit f62cd09 into main Dec 3, 2024
4 checks passed
@dellis1972 dellis1972 deleted the dev/dellis1972/activityalias branch December 3, 2024 16:38
pjcollins pushed a commit that referenced this pull request Dec 3, 2024
Context https://developer.android.com/guide/topics/manifest/activity-alias-element.
Fixes https://dev.azure.com/devdiv/DevDiv/_workitems/edit/2305723.

So activity-alias allows users to change some settings before calling an activity.
The idea is that your activity is no longer the main launcher, you still have that entry, but the activity-alias' become the main launchers.
Here is a sample AndroidManifest.xml

```xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
	<application
		android:allowBackup="true"
		android:icon="@mipmap/appicon"
		android:roundIcon="@mipmap/appicon_round"
		android:supportsRtl="true">
		<activity-alias
			android:name=".MainActivityAlias"
			android:enabled="true"
			android:icon="@mipmap/appicon"
			android:targetActivity=".MainActivity"
			android:exported="true">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity-alias>
		<activity-alias
			android:name=".MainActivityAlias2"
			android:enabled="false"
			android:icon="@mipmap/appicon2"
			android:targetActivity=".MainActivity"
			android:exported="true">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity-alias>
	</application>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
</manifest>
```
The MainActivity.cs then becomes

```csharp
[Activity(Theme = "@style/Maui.SplashTheme", LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    [Register("com.companyname.mauiappwithmultipleicons.MainActivity")]
    public class MainActivity : MauiAppCompatActivity
```
Note that we do not use the MainLauncher property on the ActivityAttribute.
jonpryor pushed a commit to dotnet/java-interop that referenced this pull request Dec 9, 2024
Changes: dotnet/android-tools@60fae19...f62cd09

  * dotnet/android-tools@f62cd09: Add support to discovering activity-alias activity elements (dotnet/android-tools#247)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants