Thanks! I’d already checked in locals() and globals() looking for some place where the running view was still accessible; I couldn’t find it. I double-checked again and it still isn’t there.
Capturing only the necessary exception makes sense, but for me I needed to capture AttributeError.
Another thing I tried to do was check that builtins.navigation was an instance of StuffView, my own subclass of ui.View, so as to be even more certain that the saved view I’m finding is the view for this app. But isinstance returned False; I’m assuming this is because the StuffView class was not in locals/globals, and so I had to recreate it; but once recreated, it isn’t the same StuffView that the previous run of Pythonista created navigation from. Whereas it is the same ui.View that each incarnation’s StuffView inherits from.
Here’s my current code to restore my views from a previous run if they exist:try: navigation = builtins.navigation except AttributeError: navigation = None if navigation and isinstance(navigation, ui.View) and navigation.on_screen: reviewView = navigation.subviews inventoryView = navigation.subviews else: reviewView = ui.load_view('views/reviews') inventoryView = ui.load_view('views/inventory') reviewView.flex = 'WH' inventoryView.flex = 'WH' if isPhone: inventoryView.remove_subview(inventoryView['kinds']) titleView = inventoryView['titles'] titleView.width = inventoryView.width-12 navigation = StuffView(frame=inventoryView.frame, name='Books & Stuff') navigation.add_subview(reviewView) navigation.add_subview(inventoryView) navigation.present() builtins.navigation = navigation
One of the things I’m assuming here is that .add_subview will always add subviews in the same order. I couldn’t find any means of getting a subview back by name that wouldn’t have been more work than just saving the subviews on builtins.