Is there any other better way to terminate Xamarin Forms App ? Than this ?
In Main Project PCL
protected override bool OnBackButtonPressed() { Device.BeginInvokeOnMainThread(async () => { var result = await this.DisplayAlert("Alert!", "want to exit?", "Yes", "No"); if (result) { var closer = DependencyService.Get<ICloseApplication>(); closer?.closeApplication(); } }); return true; }
public interface ICloseApplication { void closeApplication(); }
[assembly: Xamarin.Forms.Dependency(typeof(CloseApplication))] namespace XForms.Droid { public class CloseApplication : ICloseApplication { public void closeApplication() { Android.OS.Process.KillProcess(Android.OS.Process.MyPid()); } } }
[assembly: Xamarin.Forms.Dependency(typeof(CloseApplication))] namespace XForms.Droid { public class CloseApplication : ICloseApplication { public void closeApplication() { Thread.CurrentThread.Abort(); } } }
Posts
What are the benefits of killing the app? I'm not sure about Android, but theres no chance of Apple accepting an application to the store with this line in it
Thread.CurrentThread.Abort();
. Apple haven't made an API available to force close the app.@seanyda
Same thoughts Here ! can you give me a solution to properly exit App ?
There isn't one. You're not suppose to exit the app, You need to find out what you're trying to get out of restarting the app and handle that in code so a restart isn't required.
So Why So Many native App has "Double tap to Exit ? "
They don't. Apple wouldn't accept an app which force closes an app because theres not an API available to do it, so the only way would be to kill the thread or force throw an exception and that's no good.
So Why So Many native App has "Double tap to Exit ? " > @seanyda said:
Yep i Accept that, What about Android ?
If the above code works for Android, keep it. Theres no nice way of killing the app because it's the wrong way to go about things. You need to find a solution to what you're trying to achieve from restarting the app ideally.
In Xamarin.Forms 3.6.0 I am using, for the Android hardware back button,
@seanyda
I believe is perfectly correct - there are no benefits in killing an app ... from within. Doing this essentially defeats the object of writing the app in the first place. Even if the app cannot continue because of some catastrophic event elsewhere in the system e.g. network failure then it should simply 'tread water' until that catastrophic event is restored e.g. network available again. However if your app (as an active sender) has made contact with another external system (as a passive receiver) across say, a network, and it is terminated by some external action - perhaps by being uninstalled - then it is incumbent on the active sender app to flag/signal/communicate its impending termination to any and all external passive receivers; its rude not to say "Good-bye"! And for that purpose an OnTermination/OnShutdown/OnExit event is needed. WPF can do it (!) so why not Xamarin Forms and in particular why not UWP?