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
-
Hi,
i am always getting problems to close an app from the home screen button. If i test the script in the pythonista app everything works fine and i can close the app. But if i add a button for the app on Homescreen then the mainscreen open twice and after pushing the Exit button it only close the first screen and not the pythonista app. Can someone tell me how to really close an app?
Thanks a lot.
Cu kami
def appclose(): v.close() nav.close() os._exit(0)
-
@kami I use
os.abort()
-
Hi, thanks a lot. But this is not closing Pythonista when i run the script from the Homescreen?
-
how about
import objc_util objc_util.UIApplication.new()
-
@kami What do you call "close Pythonista"?
-
Hi,
can you both please explain what you mean. I just add an tap on the Home Screen from Pythonista. But if i use os.abort() or something else the app is closed but Pythonista is still running.
Thanks a lot.
Cu kami
-
when you say "close an app" you mean close a ui.View that was presented in a script?
-
Hi, i open with a script an ui-View with Nav-Controller. In the Nav-Controller there is an Exit Button. This button should close the app (script running in Pythonista). Temporaly it only close the script and not the Pythonista app.
-
@kami in iOS, we never can close an app... It is still in memory until you manually remove it.
-
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/7basically, 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 .
-
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
-
@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.
-
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.
-
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
-
@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.
-
@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.
-
-
@cvp, do I understand correctly that the need to relaunch Pythonista depends on how your script is launched?
-
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.
-