Skip to content
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

Android back button override closes app #2815

Closed
neilt6 opened this issue Aug 19, 2022 · 7 comments · Fixed by #2889
Closed

Android back button override closes app #2815

neilt6 opened this issue Aug 19, 2022 · 7 comments · Fixed by #2889
Milestone

Comments

@neilt6
Copy link

neilt6 commented Aug 19, 2022

The Android back button override from PrismLibrary/Prism.Maui#88 isn't working properly on my end.

Steps to Reproduce

  1. Build and launch PrismMauiDemo on an Android 10.0 emulator.
  2. Click Flyout Page.
  3. Click the Back button on the emulator.

Expected Behavior

  • Navigation from ViewD to ViewC:
[DOTNET] ViewD - View Disappearing
[DOTNET] ViewC - View Appearing
[DOTNET] ViewD - ViewModel NavigatedFrom
[DOTNET] ViewC - ViewModel NavigatedTo
[DOTNET] Navigation: GoBack
[DOTNET] Result: Success

Observed Behavior

  • The app closes, and all manner of badness ensues:
[DOTNET] ViewD - View Disappearing
[DOTNET] ViewC - View Appearing
[DOTNET] ViewC - View Disappearing
[DOTNET] ViewD - ViewModel NavigatedFrom
[DOTNET] ViewC - ViewModel NavigatedTo
[DOTNET] Navigation: GoBack
[DOTNET] Result: Success
[DOTNET] ViewC - ViewModel NavigatedFrom
[DOTNET] ViewB - ViewModel NavigatedTo
[DOTNET] Navigation: GoBack
[DOTNET] Result: Success
  • Reopening the app from recent apps reveals ViewB with no navigation bar:

[DOTNET] ViewB - View Appearing

  • Reopening the app from the launcher throws an exception and crashes:
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NotImplementedException: Either set MainPage or override CreateWindow.
[mono-rt]    at Microsoft.Maui.Controls.Application.CreateWindow(IActivationState activationState) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Application\Application.Impl.cs:line 126
[mono-rt]    at Microsoft.Maui.Controls.Application.Microsoft.Maui.IApplication.CreateWindow(IActivationState activationState) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Application\Application.Impl.cs:line 40
[mono-rt]    at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(Activity activity, IApplication application, Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\ApplicationExtensions.cs:line 44
[mono-rt]    at Microsoft.Maui.MauiAppCompatActivity.OnCreate(Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.cs:line 32
[mono-rt]    at PrismMauiDemo.MainActivity.OnCreate(Bundle savedInstanceState) in C:\Users\NeilThiessen\Downloads\Prism.Maui-master\sample\PrismMauiDemo\Platforms\Android\MainActivity.cs:line 12
[mono-rt]    at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-31/mcw/Android.App.Activity.cs:line 2781
[mono-rt]    at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 121
@sact1909
Copy link

it could be the same that is happening to me when I call the GoBackAsync, for some reason is closing the app and sometimes it says "Object no set as an Instance of an object", DON'T KNOW WHY...

await NavigationService.GoBackAsync();
or
just using the Navigation Page back button

@MardorZ
Copy link

MardorZ commented Mar 22, 2023

Is there a workaround? For now i cannot use prism because of this bug. :(

@maxchu92
Copy link

maxchu92 commented Apr 11, 2023

I noticed this as I just started to move my Prism.Forms projects to MAUI + Prism as well. Using MAUI without Prism have no issue of this. I found a temporary workaround to prevent double navigating back, or close app after meddling it for a while.

Launch first page without NavigationPage

builder
	.RegisterTypes(RegisterTypes)
	.ConfigureServices(ConfigureServices)
	.OnAppStart("MainPage"); //Not putting "NavigationPage/MainPage" here

In every page, you will need to override OnBackButtonPressed to return true.

protected override bool OnBackButtonPressed() => true;

In MauiProgram.cs, you will need to override the OnBackButtonPressed as well

public static MauiApp CreateMauiApp() {
	var builder = MauiApp.CreateBuilder();

	builder
		.UseMauiApp<App>()
		.ConfigureLifecycleEvents(builder => {
#if ANDROID
			builder.AddAndroid(android => {
				android.OnBackPressed(activity => true);
			});
#endif
		})
		.UsePrism(PrismStartup.Configure);

	return builder.Build();
}

@shahbaaz90
Copy link

@dansiegel i have tried the workaround mentioned by @maxchu92 above, but it's pretty hacky. This issue seems to point that the NavigatonService is broken for android. would love a patch for this, as Android app is unusable with this.

happens with even the sample e2e app when used with the 8.1.273-pre nuget (instead of the embedded prism Project).

@soroshsabz
Copy link

Any update?

@dansiegel
Copy link
Member

It's being looked at... and while this exact problem I could fix right now there is another another issue with the back button which isn't so easy to address. It will be fixed when it is fixed. If someone from the community wants to spend the time getting things to work for this we do take PRs, otherwise you will need to wait until I have the time to come up with a fix.

@dansiegel dansiegel added this to the Prism 9.0 milestone Jun 12, 2023
@rthanga1
Copy link

rthanga1 commented Jun 13, 2023

@maxchu92 I tried your workaround. Now, the hardware back button is working as expected. But if I press the back button in my navigation bar, it is not working. Is there anything I can try?

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

Successfully merging a pull request may close this issue.

8 participants