Skip to content

Commit 604530d

Browse files
committed
Add Android lifecycle events
1 parent 6e5a814 commit 604530d

File tree

3 files changed

+134
-3
lines changed

3 files changed

+134
-3
lines changed

apps/android_pythonnative_3/app/src/main/java/com/pythonnative/pythonnative/MainActivity.kt

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.widget.LinearLayout
1313
import androidx.constraintlayout.widget.ConstraintLayout
1414
import androidx.recyclerview.widget.RecyclerView
1515
import com.chaquo.python.PyException
16+
import com.chaquo.python.PyObject
1617
import com.chaquo.python.Python
1718
import com.chaquo.python.android.AndroidPlatform
1819
import kotlinx.coroutines.CoroutineScope
@@ -22,8 +23,13 @@ import kotlinx.coroutines.withContext
2223
import org.json.JSONObject
2324

2425
class MainActivity : AppCompatActivity() {
26+
private val TAG = javaClass.simpleName
27+
private lateinit var page: PyObject
28+
2529
override fun onCreate(savedInstanceState: Bundle?) {
2630
super.onCreate(savedInstanceState)
31+
Log.d(TAG, "onCreate() called")
32+
2733
// setContentView(R.layout.activity_main)
2834
// val layoutMain = findViewById<ConstraintLayout>(R.id.layout_main)
2935

@@ -33,10 +39,17 @@ class MainActivity : AppCompatActivity() {
3339
}
3440
val py = Python.getInstance()
3541

36-
val pyModule = py.getModule("app/main")
37-
val pyLayout = pyModule.callAttr("main", this).toJava(View::class.java)
42+
// Create an instance of the Page class
43+
val pyModule = py.getModule("app/main_2")
44+
page = pyModule.callAttr("Page", this)
45+
46+
val pyLayout = page.callAttr("on_create").toJava(View::class.java)
3847
setContentView(pyLayout)
3948

49+
// val pyModule = py.getModule("app/main")
50+
// val pyLayout = pyModule.callAttr("on_create", this).toJava(View::class.java)
51+
// setContentView(pyLayout)
52+
4053
// val createButtonModule = py.getModule("create_button")
4154
// val pyButton = createButtonModule.callAttr("create_button", this).toJava(Button::class.java)
4255
// layoutMain.addView(pyButton)
@@ -76,4 +89,52 @@ class MainActivity : AppCompatActivity() {
7689
// }
7790
// }
7891
}
92+
93+
override fun onStart() {
94+
super.onStart()
95+
Log.d(TAG, "onStart() called")
96+
page.callAttr("on_start")
97+
}
98+
99+
override fun onResume() {
100+
super.onResume()
101+
Log.d(TAG, "onResume() called")
102+
page.callAttr("on_resume")
103+
}
104+
105+
override fun onPause() {
106+
super.onPause()
107+
Log.d(TAG, "onPause() called")
108+
page.callAttr("on_pause")
109+
}
110+
111+
override fun onStop() {
112+
super.onStop()
113+
Log.d(TAG, "onStop() called")
114+
page.callAttr("on_stop")
115+
}
116+
117+
override fun onDestroy() {
118+
super.onDestroy()
119+
Log.d(TAG, "onDestroy() called")
120+
page.callAttr("on_destroy")
121+
}
122+
123+
override fun onRestart() {
124+
super.onRestart()
125+
Log.d(TAG, "onRestart() called")
126+
page.callAttr("on_restart")
127+
}
128+
129+
override fun onSaveInstanceState(outState: Bundle) {
130+
super.onSaveInstanceState(outState)
131+
Log.d(TAG, "onSaveInstanceState() called")
132+
page.callAttr("on_save_instance_state")
133+
}
134+
135+
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
136+
super.onRestoreInstanceState(savedInstanceState)
137+
Log.d(TAG, "onRestoreInstanceState() called")
138+
page.callAttr("on_restore_instance_state")
139+
}
79140
}

apps/android_pythonnative_3/app/src/main/python/app/main.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pythonnative as pn
22

33

4-
def main(context):
4+
def on_create(context):
55
stack_view = pn.StackView(context)
66

77
# label = pn.Label(context, "This is a PythonNative label")
@@ -75,3 +75,35 @@ def main(context):
7575
# stack_view.add_view(button)
7676

7777
return stack_view.native_instance
78+
79+
80+
def on_start():
81+
print("on_start() called")
82+
83+
84+
def on_resume():
85+
print("on_resume() called")
86+
87+
88+
def on_pause():
89+
print("on_pause() called")
90+
91+
92+
def on_stop():
93+
print("on_stop() called")
94+
95+
96+
def on_destroy():
97+
print("on_destroy() called")
98+
99+
100+
def on_restart():
101+
print("on_restart() called")
102+
103+
104+
def on_save_instance_state():
105+
print("on_save_instance_state() called")
106+
107+
108+
def on_restore_instance_state():
109+
print("on_restore_instance_state() called")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pythonnative as pn
2+
3+
4+
class Page:
5+
def __init__(self, context):
6+
self.context = context
7+
8+
def on_create(self):
9+
print("on_create() called")
10+
stack_view = pn.StackView(self.context)
11+
material_button = pn.MaterialButton(self.context, "MaterialButton")
12+
stack_view.add_view(material_button)
13+
# Create and add other views to the stack_view here
14+
return stack_view.native_instance
15+
16+
def on_start(self):
17+
print("on_start() called")
18+
19+
def on_resume(self):
20+
print("on_resume() called")
21+
22+
def on_pause(self):
23+
print("on_pause() called")
24+
25+
def on_stop(self):
26+
print("on_stop() called")
27+
28+
def on_destroy(self):
29+
print("on_destroy() called")
30+
31+
def on_restart(self):
32+
print("on_restart() called")
33+
34+
def on_save_instance_state(self):
35+
print("on_save_instance_state() called")
36+
37+
def on_restore_instance_state(self):
38+
print("on_restore_instance_state() called")

0 commit comments

Comments
 (0)