omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Close app from home screen button

    Pythonista
    5
    30
    12699
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cvp
      cvp @kami last edited by

      @kami in iOS, we never can close an app... It is still in memory until you manually remove it.

      1 Reply Last reply Reply Quote 1
      • JonB
        JonB last edited by

        ok, i think we have a terminology problem...

        you wrote a script, which you are calling an app. we thought you meant the app called Pythonista.

        can you share your script, or a scaled down example?
        I suspect part of your problem is that there is an issue whereby when you run a script from the home screen, it runs the script twice. so, when you close your view, there is another copy just behind it....

        someone had a workaround for that... see TPO's workarounds here:
        https://forum.omz-software.com/topic/5440/prevent-duplicate-launch-from-shortcut/7

        basically, until this bug is fixed, you need to check if your script is already running before launching it. i think there is another approach where you search through the gc.garbage for ui.View's that are on_screen, and close those .

        1 Reply Last reply Reply Quote 0
        • kami
          kami last edited by

          Hi,

          you are right. I wrote a script. And it opens twice. I now implemented the AppSingleLaunch module. In Pythonista directly everything runs fine. But if i add a Homebutton the Mainview opens but if i try to switch with the navigationview to another view the script always tells that nav view and the loaded view is not defined.

          Can you help me?

          class MyViewapp (ui.View):
              # Muss beim Einsatz von PYUI-Dateien in der View als custom_class angelegt werden.
              
              def __init__(self):
                  # This will also be called without arguments when the view is loaded from a UI file.
                  # You don't have to call super. Note that this is called *before* the attributes
                  # defined in the UI file are set. Implement `did_load` to customize a view after
                  # it's been fully loaded from a UI file.
                  
                  pass
          
              def did_load(self):
                  # This will be called when a view has been fully loaded from a UI file.
                  pass
          
              def will_close(self):
                  # This will be called when a presented view is about to be dismissed.
                  # You might want to save data here.
                  global app
                  app.will_close()
                  
                  pass
          
              def draw(self):
                  # This will be called whenever the view's content needs to be drawn.
                  # You can use any of the ui module's drawing functions here to render
                  # content into the view's visible rectangle.
                  # Do not call this method directly, instead, if you need your view
                  # to redraw its content, call set_needs_display().
                  # Example:
                  #print (v.width)
          
          
                  pass
          
              def layout(self):
                  # This will be called when a view is resized. You should typically set the
                  # frames of the view's subviews here, if your layout requirements cannot
                  # be fulfilled with the standard auto-resizing (flex) attribute.
                       
                  pass
          
              def touch_began(self, touch):
                  # Called when a touch begins.
                  pass
          
              def touch_moved(self, touch):
                  # Called when a touch moves.
                  pass
          
              def touch_ended(self, touch):
                  # Called when a touch ends.
                  pass
          
              def keyboard_frame_will_change(self, frame):
                  # Called when the on-screen keyboard appears/disappears
                  # Note: The frame is in screen coordinates.
                  pass
          
              def keyboard_frame_did_change(self, frame):
                  # Called when the on-screen keyboard appears/disappears
                  # Note: The frame is in screen coordinates.
                  pass 
                  
          def appclose():
              global nav,v,app
              
             
              app.will_close()
              v.close()
              nav.close()
              
              #os.abort()
              #sys.exit(0)
              #os._exit(0)
              
              
          
          app = AppSingleLaunch("Demo app 2")
                                       
          if not app.is_active():
              v = ui.load_view('mainview') # Customclass of MyViewapp
              
              
              v.right_button_items = [ui.ButtonItem(title="Ende", action=lambda x: appclose())]
              
              nav = ui.NavigationView(v)
              nav.name="nav"
              app.will_present(nav)
              nav.present(hide_title_bar=True)```
          
          Thanks a lot.
          
          cu kami
          mikael 1 Reply Last reply Reply Quote 0
          • mikael
            mikael @kami last edited by

            @kami, just a reminder that you can avoid the double launch by opening the script with Apple Shortcuts, and you can place the shortcut on the home screen.

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              For the not defined variables, the issue is that globals are being cleared. @shinyformica recently resolved a similar issue .

              https://forum.omz-software.com/topic/5440/prevent-duplicate-launch-from-shortcut/12

              You have to implement the don't-clear-globals bit in pythonista_startup.

              1 Reply Last reply Reply Quote 0
              • kami
                kami last edited by

                Hi,

                thanks a lot. This works fine and now there is no missing nav or v element. There are also no double views. But there is still the Problem, that when i run the script from the home shortcut button and close it. Pythonista is still active in the background and i can not restart the app from the home shortcut but. I have to close manually pythonista from the ios screen and then i can restart the script.

                This is no my problem.

                Thanks a lot.

                cu kami

                mikael 1 Reply Last reply Reply Quote 0
                • mikael
                  mikael @kami last edited by

                  @kami, not sure which of the options discussed in this thread you ended up using, but I launch the script from a Apple Shortcuts shortcut on the home screen. The shortcut uses a ”pythonista3” URL, and I can launch it repeatedly without restarting Pythonista.

                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @mikael last edited by

                    @mikael I think it depends of what your script does. I have a script that displays a MKMapView with 1200 customized pin's and I need to remove Pythonista at each run.

                    mikael 1 Reply Last reply Reply Quote 0
                    • ccc
                      ccc last edited by

                      https://medium.com/zendesk-engineering/hunting-for-memory-leaks-in-python-applications-6824d0518774

                      1 Reply Last reply Reply Quote 0
                      • mikael
                        mikael @cvp last edited by

                        @cvp, do I understand correctly that the need to relaunch Pythonista depends on how your script is launched?

                        cvp 1 Reply Last reply Reply Quote 0
                        • ccc
                          ccc last edited by

                          I suspect that the script is the impressive https://github.com/cvpe/Pythonista-scripts/blob/master/contacts on map.py and that memory is being allocated but not being released.

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @mikael last edited by cvp

                            @mikael no, no. It depends on which script. Sometimes, when I launch some big scripts using a lot of memory, as @ccc said, even if I run it in Pythonista, I can't rerun it without removing Pythonista. But, it is sometimes, not always.

                            1 Reply Last reply Reply Quote 1
                            • cvp
                              cvp @ccc last edited by cvp

                              @ccc No, no. The script showing my contacts on a map is not a big script and I don't have 1200 contacts (happily). It is a very big script (at least for me) of 7500 lines written for a friend who is "radio amateur" and showing the 1200 contacts he has realized with other people in the world. Each pin has an associated DetailCalloutAccessoryView with address, flag and other informations.
                              And, seeing the two rows of menu buttons, you can imagine that the program offers a lot of functionalities

                              mikael 1 Reply Last reply Reply Quote 1
                              • mikael
                                mikael @cvp last edited by

                                @cvp, oh my goodness, that looks cool!

                                cvp 1 Reply Last reply Reply Quote 1
                                • cvp
                                  cvp @mikael last edited by

                                  @mikael yes sir, and even 3D FlyOver. And all that thanks to Pythonista and some marvelous modules like Gestures, WKWebView of a guy called @mikael 😂

                                  1 Reply Last reply Reply Quote 0
                                  • kami
                                    kami last edited by

                                    Hi,

                                    thanks a lot for all the answer. But this doesn't solve my problem. So for now i made a simple example for everyone which will show my problem. I created a python script like this:

                                    import ui
                                    import os 
                                    
                                    def close(sender):
                                        v.close()
                                        os.abort()
                                        
                                    v = ui.load_view()
                                    v.present('sheet')
                                    

                                    and a pyui:

                                    [
                                      {
                                        "nodes" : [
                                          {
                                            "nodes" : [
                                    
                                            ],
                                            "frame" : "{{95, 162}, {80, 55}}",
                                            "class" : "Button",
                                            "attributes" : {
                                              "action" : "close",
                                              "frame" : "{{95, 217}, {80, 32}}",
                                              "title" : "Button",
                                              "uuid" : "B05F9F96-41D2-41C1-B4E1-C27F68D5CD4A",
                                              "class" : "Button",
                                              "name" : "button1",
                                              "font_size" : 15
                                            },
                                            "selected" : true
                                          }
                                        ],
                                        "frame" : "{{0, 0}, {270, 466}}",
                                        "class" : "View",
                                        "attributes" : {
                                          "enabled" : true,
                                          "background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
                                          "tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
                                          "border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
                                          "flex" : ""
                                        },
                                        "selected" : false
                                      }
                                    ]
                                    

                                    With this script and UI i just add a Button on the Home screen like it is described in Pythonista.

                                    Then i close all app on my iphone. I press the button and the App is opening twice. I press the Button in the UI and it closes. I see the normal Iphone homescren. If i now press the Home screen Button of the App (the shortcut), i only see a screen from Pythonista with a complete green background. I have to close manually from the IOS all pythonista screen and after this i can normally start the shortcut of the script again. So how can help me with this.

                                    Thanks a lot.

                                    cu kami

                                    cvp mikael 2 Replies Last reply Reply Quote 0
                                    • cvp
                                      cvp @kami last edited by

                                      @kami perhaps the same as known issue here

                                      1 Reply Last reply Reply Quote 0
                                      • mikael
                                        mikael @kami last edited by

                                        @kami, just to rule out potential issues, can you confirm that you tried launching your script via a home screen icon that you created with the Apple Shortcuts app?

                                        I used this simple script, called test-launch.py:

                                        import ui, os
                                        
                                        class SuperClose(ui.View):
                                            
                                            def will_close(self):
                                                os.abort()
                                                
                                        v = SuperClose(background_color='red')
                                        
                                        v.present()
                                        

                                        And this URL in the Shorcuts app:

                                        pythonista3://test-launch?action=run
                                        

                                        And the script is launched only once and Pythonista goes away when I close the view.

                                        cvp 1 Reply Last reply Reply Quote 0
                                        • cvp
                                          cvp @mikael last edited by cvp

                                          @mikael I think he uses omz' script for creating an icon on home screen (here) and in this case, tapping the icon starts twice the app.

                                          1 Reply Last reply Reply Quote 0
                                          • kami
                                            kami last edited by

                                            HI, i will try to create an Apple Shortcut. But can someone please tell me how to write the path if the files are stored in the icloud?

                                            Thanks a lot.

                                            Cu kami

                                            cvp 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors