Maui
Maui copied to clipboard
[BUG] StatusBarBehavior throws "Android Activity can't be null" exception if used in Page without Shell.
Is there an existing issue for this?
- [X] I have searched the existing issues
Current Behavior
I am changing the StatusBar color using this sample: https://github.com/jfversluis/MauiToolkitStatusBarBehaviorSample in .NET Maui project.
However, the sample works good if "Shell" is used in Android.
But instead, if we load the first page without Shell, then it crashes with error "Android activity can't be null" in android platform.
Expected Behavior
The StatusBarBehavior should also work with Page without Shell and should not throw exception: "Android Activity can't be null"
Steps To Reproduce
- Download sample from : https://github.com/jfversluis/MauiToolkitStatusBarBehaviorSample
- in App.xaml.cs change to :
MainPage = new NavigationPage(new MainPage());
- Run in Android.
- You will see this exception:
Link to public reproduction project repository
https://github.com/jfversluis/MauiToolkitStatusBarBehaviorSample
Environment
- .NET MAUI CommunityToolkit:1.3.0
- OS: Android
Anything else?
@MGohil can you move your code to OnNavigatedFrom
or OnAppearing
?
@pictos : Yes it works if I put this into OnAppearing.
But this means that, we can not set the StatusBarColor and StatusBarStyle from XAML page itself. We have to do this in code behind.
protected override void OnAppearing()
{
base.OnAppearing();
statusBar.StatusBarColor = Colors.Gray;
statusBar.StatusBarStyle = CommunityToolkit.Maui.Core.StatusBarStyle.DarkContent;
}
As per what @jfversluis mentioned in one of the YouTube videos https://www.youtube.com/watch?v=dWj0PdImH10, it would be great if this works as expected in XAML as well.
Thanks.
@MGohil thanks for testing that, so you can use the behavior on the code-behind, and that probably will work as you want.
The long answer will be: The lifecycle of .NET MAUI is, IMHO, not great. But they choose to be this way to no break the Xamarin.Forms apps that will migrate to .NET MAUI, so you know, trade-offs.
When the shared layer is created and initialized the platform-specific isn't, so you can get these issues. In other words, when the ContentPage.ctor
is called the AndroidContext isn't created yet, so you will hit the NRE. Now if this isn't your MainPage, that will work if you set things on your ctor
because for .NET MAUI there's one Activity and one global Context. Now if you open a new Window
have no idea how that will behave.
I would say this isn't on us, maybe we can add some defensive code on that (?), but for now, what I can say, is don't set platform-specific code inside shared controls ctor
s or use the Behavior
, because under the hood it uses the PlatformBehavior
API that makes sure that everything is in place before calling the platform-specific code.
Why the requirement of order with the behavior? Can't the behavior just store those colors at the xplat level and then once OnPlatformAttached is called with StatusBarBehavior it sets those?
Like, just don't fire any of this code here
https://github.com/CommunityToolkit/Maui/blob/9de9ec676972a390d534a155a4c4ac38cee2f06a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/StatusBar/StatusBarBehavior.shared.cs#L64-L81
Until PlatformAttached is called
I can confirm what @PureWeen said. Line 79 is what my App caused to crash too. Maybe it's possible to store those colors elsewhere like he said or at least there could be a try catch or some checks with some log output so at least the app doesnt crash?
System.InvalidOperationException: Android Activity can't be null. [AppCenterXamarinCrashes] at CommunityToolkit.Maui.Core.Platform.StatusBar.get_Activity() in /_/src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs:line 13 [AppCenterXamarinCrashes] at CommunityToolkit.Maui.Core.Platform.StatusBar.PlatformSetStyle(StatusBarStyle style) in /_/src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs:line 38 [AppCenterXamarinCrashes] at CommunityToolkit.Maui.Core.Platform.StatusBar.SetStyle(StatusBarStyle statusBarStyle) in /_/src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.shared.cs:line 20 [AppCenterXamarinCrashes] at CommunityToolkit.Maui.Behaviors.StatusBarBehavior.OnPropertyChanged(String propertyName) in /_/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/StatusBar/StatusBarBehavior.shared.cs:line 79
I am using StatusBarBehavior in XAML and it works but not with AppThemeBinding