Skip to content

Move android app initialization to custom application class #18756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2025

Conversation

emmauss
Copy link
Contributor

@emmauss emmauss commented Apr 29, 2025

What does the pull request do?

Adds an Application class to the android backend and moving initialization to it. This ensures that no matter which activity is launched, the avalonia subsystem will be initialized with the correct App class.

What is the current behavior?

App initialization on android only occurs when the main activity is launched. Any other activity launched before or without the main activity will use the default avalonia Application, thus styles and resources will not get loaded. I'm guessing this behaviour was added just to get the android backend up and running, and hasn't been fixed since.

What is the updated/expected behavior with this PR?

Avalonia App is initialized once in a process, and any activity launched hosting an avalonia control will have access to app styles.

How was the solution implemented (if it's not obvious)?

Checklist

Breaking changes

All android projects targeting nightly or v12 will have to be updated. AvaloniaMainActivity<TApp> is removed in favor of adding an Application class that derives from the AvaloniaAndroidApplication<TApp> class.

Obsoletions / Deprecations

Fixed issues

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0056258-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Copy link
Member

@MrJul MrJul left a comment

Choose a reason for hiding this comment

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

Don't forget to add the breaking change to https://github.com/AvaloniaUI/Avalonia/wiki/v12-Breaking-Changes once the PR gets merged.

Copy link
Member

@MrJul MrJul left a comment

Choose a reason for hiding this comment

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

LGTM!

@MrJul MrJul enabled auto-merge May 9, 2025 09:09
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0056434-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul added this pull request to the merge queue May 9, 2025
Merged via the queue into master with commit 306f37d May 9, 2025
11 checks passed
@MrJul MrJul deleted the android_application branch May 9, 2025 13:28
@kerams
Copy link
Contributor

kerams commented May 10, 2025

I've added android:name=".Application" to application in the manifest, and split MainActivity into

[<Application>]
type Application (javaRef, transfer) =
    inherit AvaloniaAndroidApplication<FabApplication> (javaRef, transfer)

    override _.CreateAppBuilder () =
        Main.configureLogging ()
        (Main.configure activated.Publish).UseAndroid ()

[<Activity(Label = "tst",
           Theme = "@style/MyTheme.Starting",
           LaunchMode = LaunchMode.SingleTop,
           MainLauncher = true,
           ConfigurationChanges = (ConfigChanges.Orientation ||| ConfigChanges.ScreenSize ||| ConfigChanges.UiMode))>]
type MainActivity () =
    inherit AvaloniaMainActivity ()

    override _.OnRequestPermissionsResult (c, p, r) =
        Platform.OnRequestPermissionsResult (c, p, r)
        base.OnRequestPermissionsResult (c, p, r)

    override this.OnCreate s =
        AndroidX.Core.SplashScreen.SplashScreen.InstallSplashScreen this |> ignore
        base.OnCreate s
        (this :> IAvaloniaActivity).Activated.Add activated.Trigger
        Platform.Init (this, s)

which as far as I can see mirrors changes to the samples in this PR. When I try to run this though, I get Android.Runtime.JavaProxyThrowable: '[System.InvalidOperationException]: Invalid Arrange rectangle.'. Am I missing something?

@emmauss
Copy link
Contributor Author

emmauss commented May 10, 2025

I've added android:name=".Application" to application in the manifest, and split MainActivity into

[<Application>]
type Application (javaRef, transfer) =
    inherit AvaloniaAndroidApplication<FabApplication> (javaRef, transfer)

    override _.CreateAppBuilder () =
        Main.configureLogging ()
        (Main.configure activated.Publish).UseAndroid ()

[<Activity(Label = "tst",
           Theme = "@style/MyTheme.Starting",
           LaunchMode = LaunchMode.SingleTop,
           MainLauncher = true,
           ConfigurationChanges = (ConfigChanges.Orientation ||| ConfigChanges.ScreenSize ||| ConfigChanges.UiMode))>]
type MainActivity () =
    inherit AvaloniaMainActivity ()

    override _.OnRequestPermissionsResult (c, p, r) =
        Platform.OnRequestPermissionsResult (c, p, r)
        base.OnRequestPermissionsResult (c, p, r)

    override this.OnCreate s =
        AndroidX.Core.SplashScreen.SplashScreen.InstallSplashScreen this |> ignore
        base.OnCreate s
        (this :> IAvaloniaActivity).Activated.Add activated.Trigger
        Platform.Init (this, s)

which as far as I can see mirrors changes to the samples in this PR. When I try to run this though, I get Android.Runtime.JavaProxyThrowable: '[System.InvalidOperationException]: Invalid Arrange rectangle.'. Am I missing something?

Please post the full stack trace and a simple reproduction

@kerams
Copy link
Contributor

kerams commented May 10, 2025

Never mind, I think #18806 is the actual problem, because I tracked the error to a TabControl from Material.Styles, which uses a UniformGrid - if I remove one of the tabs, the exception disappears.

EDIT: yeah, the build from #18832, which includes commits from this PR, fixes the problem.

Nevertheless, it's bizarre that this started manifesting after moving to 12.0.999-cibuild0056440-alpha from the previous build 12.0.999-cibuild0056436-alpha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants