-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.py
More file actions
209 lines (159 loc) · 6.91 KB
/
page.py
File metadata and controls
209 lines (159 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
Your current approach, which involves creating an Android Activity in Kotlin
and then passing it to Python, is necessary due to the restrictions inherent
in Android's lifecycle. You are correctly following the Android way of managing
Activities. In Android, the system is in control of when and how Activities are
created and destroyed. It is not possible to directly create an instance of an
Activity from Python because that would bypass Android's lifecycle management,
leading to unpredictable results.
Your Button example works because Button is a View, not an Activity. View
instances in Android can be created and managed directly by your code. This is
why you are able to create an instance of Button from Python.
Remember that Activities in Android are not just containers for your UI like a
ViewGroup, they are also the main entry points into your app and are closely
tied to the app's lifecycle. Therefore, Android needs to maintain tight control
over them. Activities aren't something you instantiate whenever you need them;
they are created in response to a specific intent and their lifecycle is
managed by Android.
So, to answer your question: Yes, you need to follow this approach for
Activities in Android. You cannot instantiate an Activity from Python like you
do for Views.
On the other hand, for iOS, you can instantiate a UIViewController directly
from Python. The example code you provided for this is correct.
Just ensure that your PythonNative UI framework is aware of these platform
differences and handles them appropriately.
"""
from abc import ABC, abstractmethod
from .utils import IS_ANDROID, set_android_context
from .view import ViewBase
# ========================================
# Base class
# ========================================
class PageBase(ABC):
@abstractmethod
def __init__(self) -> None:
super().__init__()
@abstractmethod
def set_root_view(self, view) -> None:
pass
@abstractmethod
def on_create(self) -> None:
pass
@abstractmethod
def on_start(self) -> None:
pass
@abstractmethod
def on_resume(self) -> None:
pass
@abstractmethod
def on_pause(self) -> None:
pass
@abstractmethod
def on_stop(self) -> None:
pass
@abstractmethod
def on_destroy(self) -> None:
pass
@abstractmethod
def on_restart(self) -> None:
pass
@abstractmethod
def on_save_instance_state(self) -> None:
pass
@abstractmethod
def on_restore_instance_state(self) -> None:
pass
@abstractmethod
def navigate_to(self, page) -> None:
pass
if IS_ANDROID:
# ========================================
# Android class
# https://developer.android.com/reference/android/app/Activity
# ========================================
from java import jclass
class Page(PageBase, ViewBase):
def __init__(self, native_instance) -> None:
super().__init__()
self.native_class = jclass("android.app.Activity")
self.native_instance = native_instance
# self.native_instance = self.native_class()
# Stash the Activity so child views can implicitly acquire a Context
set_android_context(native_instance)
def set_root_view(self, view) -> None:
self.native_instance.setContentView(view.native_instance)
def on_create(self) -> None:
print("Android on_create() called")
def on_start(self) -> None:
print("Android on_start() called")
def on_resume(self) -> None:
print("Android on_resume() called")
def on_pause(self) -> None:
print("Android on_pause() called")
def on_stop(self) -> None:
print("Android on_stop() called")
def on_destroy(self) -> None:
print("Android on_destroy() called")
def on_restart(self) -> None:
print("Android on_restart() called")
def on_save_instance_state(self) -> None:
print("Android on_save_instance_state() called")
def on_restore_instance_state(self) -> None:
print("Android on_restore_instance_state() called")
def navigate_to(self, page) -> None:
# intent = jclass("android.content.Intent")(self.native_instance, page.native_class)
intent = jclass("android.content.Intent")(
self.native_instance,
jclass("com.pythonnative.pythonnative.SecondActivity"),
)
self.native_instance.startActivity(intent)
else:
# ========================================
# iOS class
# https://developer.apple.com/documentation/uikit/uiviewcontroller
# ========================================
from rubicon.objc import ObjCClass, ObjCInstance
class Page(PageBase, ViewBase):
def __init__(self, native_instance) -> None:
super().__init__()
self.native_class = ObjCClass("UIViewController")
# If Swift passed us an integer pointer, wrap it as an ObjCInstance.
if isinstance(native_instance, int):
try:
native_instance = ObjCInstance(native_instance)
except Exception:
native_instance = None
self.native_instance = native_instance
# self.native_instance = self.native_class.alloc().init()
def set_root_view(self, view) -> None:
# UIViewController.view is a property; access without calling.
root_view = self.native_instance.view
# Size the root child to fill the controller's view and enable autoresizing
try:
bounds = root_view.bounds
view.native_instance.setFrame_(bounds)
# UIViewAutoresizingFlexibleWidth (2) | UIViewAutoresizingFlexibleHeight (16)
view.native_instance.setAutoresizingMask_(2 | 16)
except Exception:
pass
root_view.addSubview_(view.native_instance)
def on_create(self) -> None:
print("iOS on_create() called")
def on_start(self) -> None:
print("iOS on_start() called")
def on_resume(self) -> None:
print("iOS on_resume() called")
def on_pause(self) -> None:
print("iOS on_pause() called")
def on_stop(self) -> None:
print("iOS on_stop() called")
def on_destroy(self) -> None:
print("iOS on_destroy() called")
def on_restart(self) -> None:
print("iOS on_restart() called")
def on_save_instance_state(self) -> None:
print("iOS on_save_instance_state() called")
def on_restore_instance_state(self) -> None:
print("iOS on_restore_instance_state() called")
def navigate_to(self, page) -> None:
self.native_instance.navigationController().pushViewControllerAnimated_(page.native_instance, True)