Terminate Xamarin Forms App | Quitting Xamarin Forms App

CharwakaCharwaka INMember ✭✭✭✭✭
edited February 2018 in Xamarin.Forms

Is there any other better way to terminate Xamarin Forms App ? Than this ?

In Main Project PCL

MainPage.Xaml.cs(The Page Where i want to Terminate)

    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;
            }

Interface

         public interface ICloseApplication
            {
                void closeApplication();
            }

Android

  [assembly: Xamarin.Forms.Dependency(typeof(CloseApplication))]
namespace XForms.Droid
{
    public class CloseApplication : ICloseApplication
    {
        public void closeApplication()
        {
            Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
        }
    }
}

IOS

 [assembly: Xamarin.Forms.Dependency(typeof(CloseApplication))]
namespace XForms.Droid
{
         public class CloseApplication : ICloseApplication
                {
                    public void closeApplication()
                    {
                        Thread.CurrentThread.Abort();
                    }
                }
        }

Posts

  • seanydaseanyda GBMember ✭✭✭✭✭

    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.

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @seanyda

    Same thoughts Here ! can you give me a solution to properly exit App ?

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Charwaka said:
    @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.

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @seanyda said:

    @Charwaka said:
    @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 ? "

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Charwaka said:

    @seanyda said:

    @Charwaka said:
    @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.

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @seanyda said:

    @Charwaka said:
    @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 ? " > @seanyda said:

    @Charwaka said:

    @seanyda said:

    @Charwaka said:
    @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.

    Yep i Accept that, What about Android ?

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Charwaka said:

    @seanyda said:

    @Charwaka said:
    @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 ? " > @seanyda said:

    @Charwaka said:

    @seanyda said:

    @Charwaka said:
    @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.

    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.

  • lucidBrotlucidBrot Member ✭✭

    In Xamarin.Forms 3.6.0 I am using, for the Android hardware back button,

    protected override bool OnBackButtonPressed()
            {
                // return true if handled
                // return false to pass the event on
                Application.Current.Quit();
                return false; // if you return true here, the app won't close
            }
    
Sign In or Register to comment.