Skip to content

Commit 2eb0384

Browse files
committed
Add Page navigation
1 parent 04b298a commit 2eb0384

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

apps/android_pythonnative_3/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
android:supportsRtl="true"
1515
android:theme="@style/Theme.Pythonnative"
1616
tools:targetApi="31">
17+
<activity
18+
android:name=".SecondActivity"
19+
android:exported="false" />
1720
<activity
1821
android:name=".MainActivity"
1922
android:exported="true">
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.pythonnative.pythonnative
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.util.Log
6+
import com.chaquo.python.PyObject
7+
import com.chaquo.python.Python
8+
import com.chaquo.python.android.AndroidPlatform
9+
10+
class SecondActivity : AppCompatActivity() {
11+
private val TAG = javaClass.simpleName
12+
private lateinit var page: PyObject
13+
14+
override fun onCreate(savedInstanceState: Bundle?) {
15+
super.onCreate(savedInstanceState)
16+
Log.d(TAG, "onCreate() called")
17+
if (!Python.isStarted()) {
18+
Python.start(AndroidPlatform(this))
19+
}
20+
val py = Python.getInstance()
21+
val pyModule = py.getModule("app/second_page")
22+
page = pyModule.callAttr("SecondPage", this)
23+
page.callAttr("on_create")
24+
}
25+
26+
override fun onStart() {
27+
super.onStart()
28+
Log.d(TAG, "onStart() called")
29+
page.callAttr("on_start")
30+
}
31+
32+
override fun onResume() {
33+
super.onResume()
34+
Log.d(TAG, "onResume() called")
35+
page.callAttr("on_resume")
36+
}
37+
38+
override fun onPause() {
39+
super.onPause()
40+
Log.d(TAG, "onPause() called")
41+
page.callAttr("on_pause")
42+
}
43+
44+
override fun onStop() {
45+
super.onStop()
46+
Log.d(TAG, "onStop() called")
47+
page.callAttr("on_stop")
48+
}
49+
50+
override fun onDestroy() {
51+
super.onDestroy()
52+
Log.d(TAG, "onDestroy() called")
53+
page.callAttr("on_destroy")
54+
}
55+
56+
override fun onRestart() {
57+
super.onRestart()
58+
Log.d(TAG, "onRestart() called")
59+
page.callAttr("on_restart")
60+
}
61+
62+
override fun onSaveInstanceState(outState: Bundle) {
63+
super.onSaveInstanceState(outState)
64+
Log.d(TAG, "onSaveInstanceState() called")
65+
page.callAttr("on_save_instance_state")
66+
}
67+
68+
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
69+
super.onRestoreInstanceState(savedInstanceState)
70+
Log.d(TAG, "onRestoreInstanceState() called")
71+
page.callAttr("on_restore_instance_state")
72+
}
73+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pythonnative as pn
2+
3+
4+
class SecondPage(pn.Page):
5+
def __init__(self, native_instance):
6+
super().__init__(native_instance)
7+
8+
def on_create(self):
9+
super().on_create()
10+
stack_view = pn.StackView(self.native_instance)
11+
label = pn.Label(self.native_instance, "Label")
12+
stack_view.add_view(label)
13+
self.set_root_view(stack_view)
14+
15+
def on_start(self):
16+
super().on_start()
17+
18+
def on_resume(self):
19+
super().on_resume()
20+
21+
def on_pause(self):
22+
super().on_pause()
23+
24+
def on_stop(self):
25+
super().on_stop()
26+
27+
def on_destroy(self):
28+
super().on_destroy()
29+
30+
def on_restart(self):
31+
super().on_restart()
32+
33+
def on_save_instance_state(self):
34+
super().on_save_instance_state()
35+
36+
def on_restore_instance_state(self):
37+
super().on_restore_instance_state()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".SecondActivity">
8+
9+
</androidx.constraintlayout.widget.ConstraintLayout>

libs/pythonnative/pythonnative/page.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def on_save_instance_state(self) -> None:
8383
def on_restore_instance_state(self) -> None:
8484
pass
8585

86+
@abstractmethod
87+
def navigate_to(self, page) -> None:
88+
pass
89+
8690

8791
if IS_ANDROID:
8892
# ========================================
@@ -128,6 +132,11 @@ def on_save_instance_state(self) -> None:
128132
def on_restore_instance_state(self) -> None:
129133
print("Android on_restore_instance_state() called")
130134

135+
def navigate_to(self, page) -> None:
136+
IntentClass = jclass("android.content.Intent")
137+
intent = IntentClass(self.native_instance, page.native_class)
138+
self.native_instance.startActivity(intent)
139+
131140
else:
132141
# ========================================
133142
# iOS class
@@ -171,3 +180,7 @@ def on_save_instance_state(self) -> None:
171180

172181
def on_restore_instance_state(self) -> None:
173182
print("iOS on_restore_instance_state() called")
183+
184+
def navigate_to(self, page) -> None:
185+
self.native_instance.navigationController().pushViewControllerAnimated_(
186+
page.native_instance, True)

0 commit comments

Comments
 (0)