Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[BUG] StatusBarBehavior throws "Android Activity can't be null" exception if used in Page without Shell.

Open MGohil opened this issue 2 years ago • 3 comments

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

  1. Download sample from : https://github.com/jfversluis/MauiToolkitStatusBarBehaviorSample
  2. in App.xaml.cs change to : MainPage = new NavigationPage(new MainPage());
  3. Run in Android.
  4. You will see this exception: image

Link to public reproduction project repository

https://github.com/jfversluis/MauiToolkitStatusBarBehaviorSample

Environment

- .NET MAUI CommunityToolkit:1.3.0
- OS: Android

Anything else?

image

image

MGohil avatar Oct 11 '22 10:10 MGohil

@MGohil can you move your code to OnNavigatedFrom or OnAppearing?

pictos avatar Oct 11 '22 12:10 pictos

@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 avatar Oct 11 '22 13:10 MGohil

@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 ctors 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.

pictos avatar Oct 11 '22 14:10 pictos

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

PureWeen avatar Oct 25 '22 16:10 PureWeen

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

borrmann avatar Dec 06 '22 09:12 borrmann

I am using StatusBarBehavior in XAML and it works but not with AppThemeBinding

kaisnert avatar Dec 06 '22 13:12 kaisnert