How to fully close a Delphi-XE5 Android App?

This has been asked before but not fully answered. 

I want to exit an app for two reasons. 

1. on startup if problems with the data files and  (I'd also like to know to handle this case on IOS too!)
2. from a back-button press

Other answere here and elsewhere got answers of using:

SharedActivity.Finish;

or

MainActivity.Finish;

Both appear to close the window but neither exit the application as in debug mode the program does not end in Delphi and the application is still running on the device itself. The apps seem to eventually go away when in that state, but if you try to run it again, it hangs... that happens if its run again from the icon, or selected from the list of running apps button.

If its not possible to get this to work, how would I get the behavior that many apps have --- that is you back step through an app to the main screen, but the last back button just steps back to the home screen without closing the app.

Any ideas? The hardware that I am working on is a Nexus 4.
1
David
12/2/2013 7:18:47 PM
embarcadero.delphi.firemonkey 4901 articles. 4 followers. Follow

7 Replies
6353 Views

Similar Articles

[PageSpeed] 3

I have a solution that works for me
I have posted the code a few times before
try doing a search
> 
> Both appear to close the window but neither exit the application as in debug mode the program does not end in Delphi and the application is still running on the device itself. The apps seem to eventually go away when in that state, but if you try to run it again, it hangs... that happens if its run again from the icon, or selected from the list of running apps button.
> 
> If its not possible to get this to work, how would I get the behavior that many apps have --- that is you back step through an app to the main screen, but the last back button just steps back to the home screen without closing the app.
> 
> Any ideas? The hardware that I am working on is a Nexus 4.
0
Brian
12/3/2013 1:39:34 AM
Hi Bruce,

I presume you mean this  [thread|https://forums.embarcadero.com/thread.jspa?messageID=602223] ?

--- Dave

> {quote:title=Brian Hamilton wrote:}{quote}
> I have a solution that works for me
> I have posted the code a few times before
> try doing a search
> > 
> > Both appear to close the window but neither exit the application as in debug mode the program does not end in Delphi and the application is still running on the device itself. The apps seem to eventually go away when in that state, but if you try to run it again, it hangs... that happens if its run again from the icon, or selected from the list of running apps button.
> > 
> > If its not possible to get this to work, how would I get the behavior that many apps have --- that is you back step through an app to the main screen, but the last back button just steps back to the home screen without closing the app.
> > 
> > Any ideas? The hardware that I am working on is a Nexus 4.
0
David
12/3/2013 1:38:30 PM
Well, I tried the code below which you said works for you in my KeyUp exit procedure and it doesn't work - the generates an access violation. So, that is not a reliable method to close. If I don't get any other suggestions, soon, I'll use one my of my "precious" support cases on it. :-)

--- Dave

{code}
      if MessageDlg('Exit OH Companion?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1) = mrOK then
      begin
      {$IFDEF ANDROID}
        try
          Hide;
          Application.MainForm.DisposeOf;
        except
          on e:exception do
          begin
           Application.MainForm.DisposeOf;
          end;
        end;
      {$ENDIF}
      {$IFDEF WIN32}
        Application.Terminate;
      {$ENDIF}
      end
      else Key:=0;
{code}




> {quote:title=David Lane wrote:}{quote}
> Hi Bruce,
> 
> I presume you mean this  [thread|https://forums.embarcadero.com/thread.jspa?messageID=602223] ?
> 
> --- Dave
> 
> > {quote:title=Brian Hamilton wrote:}{quote}
> > I have a solution that works for me
> > I have posted the code a few times before
> > try doing a search
> > > 
> > > Both appear to close the window but neither exit the application as in debug mode the program does not end in Delphi and the application is still running on the device itself. The apps seem to eventually go away when in that state, but if you try to run it again, it hangs... that happens if its run again from the icon, or selected from the list of running apps button.
> > > 
> > > If its not possible to get this to work, how would I get the behavior that many apps have --- that is you back step through an app to the main screen, but the last back button just steps back to the home screen without closing the app.
> > > 
> > > Any ideas? The hardware that I am working on is a Nexus 4.
0
David
12/3/2013 6:00:28 PM
> {quote:title=David Lane wrote:}{quote}
> This has been asked before but not fully answered. 
> 
> I want to exit an app for two reasons. 
> 
> 1. on startup if problems with the data files and  (I'd also like to know to handle this case on IOS too!)
> 2. from a back-button press
> 
> Other answere here and elsewhere got answers of using:
> 
> SharedActivity.Finish;
> 
> or
> 
> MainActivity.Finish;
> 
> Both appear to close the window but neither exit the application as in debug mode the program does not end in Delphi and the application is still running on the device itself. The apps seem to eventually go away when in that state, but if you try to run it again, it hangs... that happens if its run again from the icon, or selected from the list of running apps button.
> 
> If its not possible to get this to work, how would I get the behavior that many apps have --- that is you back step through an app to the main screen, but the last back button just steps back to the home screen without closing the app.
> 
> Any ideas? The hardware that I am working on is a Nexus 4.

I used it and it worked Nexus 7.

add FMX.Platform.Android unit;

 try
    Self.Hide;
     MainActivity.finish;
     Application.MainForm.DisposeOf
     except

   on e:exception do
      begin
       Application.MainForm.DisposeOf;
      end;
   end;
0
Utf
12/19/2013 2:54:54 PM
Thanks Eder, but it still does not work properly. I tried your slightly different version on both my Nexus 4 and Nexus 7 and it appears to exit, but if you bring up the list of running apps, its still there. It may eventually disappear from there, but if you try to run the app again (from its icon), you just get a black screen. With your application to you see a different behavior? 

> {quote:title=Éder Brandão wrote:}{quote}
> > {quote:title=David Lane wrote:}{quote}
> > This has been asked before but not fully answered. 
> > 
> > I want to exit an app for two reasons. 
> > 
> > 1. on startup if problems with the data files and  (I'd also like to know to handle this case on IOS too!)
> > 2. from a back-button press
> > 
> > Other answere here and elsewhere got answers of using:
> > 
> > SharedActivity.Finish;
> > 
> > or
> > 
> > MainActivity.Finish;
> > 
> > Both appear to close the window but neither exit the application as in debug mode the program does not end in Delphi and the application is still running on the device itself. The apps seem to eventually go away when in that state, but if you try to run it again, it hangs... that happens if its run again from the icon, or selected from the list of running apps button.
> > 
> > If its not possible to get this to work, how would I get the behavior that many apps have --- that is you back step through an app to the main screen, but the last back button just steps back to the home screen without closing the app.
> > 
> > Any ideas? The hardware that I am working on is a Nexus 4.
> 
> I used it and it worked Nexus 7.
> 
> add FMX.Platform.Android unit;
> 
>  try
>     Self.Hide;
>      MainActivity.finish;
>      Application.MainForm.DisposeOf
>      except
> 
>    on e:exception do
>       begin
>        Application.MainForm.DisposeOf;
>       end;
>    end;
0
David
12/20/2013 1:40:31 AM
Try using form.destroy;
> {quote:title=David Lane wrote:}{quote}
> Well, I tried the code below which you said works for you in my KeyUp exit procedure and it doesn't work - the generates an access violation. So, that is not a reliable method to close. If I don't get any other suggestions, soon, I'll use one my of my "precious" support cases on it. :-)
> 
> --- Dave
> 
> {code}
>       if MessageDlg('Exit OH Companion?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1) = mrOK then
>       begin
>       {$IFDEF ANDROID}
>         try
>           Hide;
>           Application.MainForm.DisposeOf;
>         except
>           on e:exception do
>           begin
>            Application.MainForm.DisposeOf;
>           end;
>         end;
>       {$ENDIF}
>       {$IFDEF WIN32}
>         Application.Terminate;
>       {$ENDIF}
>       end
>       else Key:=0;
> {code}
> 
> 
> 
> 
> > {quote:title=David Lane wrote:}{quote}
> > Hi Bruce,
> > 
> > I presume you mean this  [thread|https://forums.embarcadero.com/thread.jspa?messageID=602223] ?
> > 
> > --- Dave
> > 
> > > {quote:title=Brian Hamilton wrote:}{quote}
> > > I have a solution that works for me
> > > I have posted the code a few times before
> > > try doing a search
> > > > 
> > > > Both appear to close the window but neither exit the application as in debug mode the program does not end in Delphi and the application is still running on the device itself. The apps seem to eventually go away when in that state, but if you try to run it again, it hangs... that happens if its run again from the icon, or selected from the list of running apps button.
> > > > 
> > > > If its not possible to get this to work, how would I get the behavior that many apps have --- that is you back step through an app to the main screen, but the last back button just steps back to the home screen without closing the app.
> > > > 
> > > > Any ideas? The hardware that I am working on is a Nexus 4.
0
alap
3/25/2014 1:27:08 PM
Hello, i'm new on this forum and sorry for my english. I have this same problem on my android app, something i discover is that when you close the app (using several things like "mainactivity.finish" and others) the app close but something that keeps running is data in cache.

if i clear the cache, android lets me running the app again.
David, did you get the solution for this problem?

good luck to all!

> {quote:title=David Lane wrote:}{quote}
> Thanks Eder, but it still does not work properly. I tried your slightly different version on both my Nexus 4 and Nexus 7 and it appears to exit, but if you bring up the list of running apps, its still there. It may eventually disappear from there, but if you try to run the app again (from its icon), you just get a black screen. With your application to you see a different behavior? 
> 
> > {quote:title=Éder Brandão wrote:}{quote}
> > > {quote:title=David Lane wrote:}{quote}
> > > This has been asked before but not fully answered. 
> > > 
> > > I want to exit an app for two reasons. 
> > > 
> > > 1. on startup if problems with the data files and  (I'd also like to know to handle this case on IOS too!)
> > > 2. from a back-button press
> > > 
> > > Other answere here and elsewhere got answers of using:
> > > 
> > > SharedActivity.Finish;
> > > 
> > > or
> > > 
> > > MainActivity.Finish;
> > > 
> > > Both appear to close the window but neither exit the application as in debug mode the program does not end in Delphi and the application is still running on the device itself. The apps seem to eventually go away when in that state, but if you try to run it again, it hangs... that happens if its run again from the icon, or selected from the list of running apps button.
> > > 
> > > If its not possible to get this to work, how would I get the behavior that many apps have --- that is you back step through an app to the main screen, but the last back button just steps back to the home screen without closing the app.
> > > 
> > > Any ideas? The hardware that I am working on is a Nexus 4.
> > 
> > I used it and it worked Nexus 7.
> > 
> > add FMX.Platform.Android unit;
> > 
> >  try
> >     Self.Hide;
> >      MainActivity.finish;
> >      Application.MainForm.DisposeOf
> >      except
> > 
> >    on e:exception do
> >       begin
> >        Application.MainForm.DisposeOf;
> >       end;
> >    end;
0
Jonathan
4/16/2014 12:41:54 PM
Reply:

Web resources about - How to fully close a Delphi-XE5 Android App? - embarcadero.delphi.firemonkey

Resources last updated: 2/8/2016 6:42:20 PM