'How can i use exact functionality for android back button by kivymd
When i long pressing the back button in last screen of my application then all the screen is being back and close my application. I want to release the back key and screen should be back. How can i use exact functionality for android back button by kivymd? Please someone help me.
Here is my code.
main.py
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.lang import Builder
KV = '''
ScreenManager:
CK1:
CK2:
CK3:
<CK1>:
name: 'C1'
MDScreen:
md_bg_color: [23/255, 200/255, 230/255, 1]
MDFillRoundFlatButton:
text: "Go screen2"
size_hint_y:.06
size_hint_x: .95
pos_hint: {"center_x": 0.5, "center_y": 0.80}
md_bg_color: [147/255, 186/255, 250/255, 1]
font_size: 20
text_color: [255/255, 255/255, 0/255, 1]
on_release: root.manager.current = 'C2'
on_release: root.manager.transition.direction = 'left'
on_release: root.manager.transition.duration = .3
<CK2>:
name: 'C2'
MDScreen:
md_bg_color: [231/255, 231/255, 231/255, 1]
MDFillRoundFlatButton:
text: "Go screen3"
size_hint_y:.06
size_hint_x: .95
pos_hint: {"center_x": 0.5, "center_y": 0.80}
md_bg_color: [147/255, 186/255, 250/255, 1]
font_size: 20
text_color: [255/255, 255/255, 0/255, 1]
on_release: root.manager.current = 'C3'
on_release: root.manager.transition.direction = 'left'
on_release: root.manager.transition.duration = .3
<CK3>:
name: 'C3'
MDScreen:
md_bg_color: [231/255, 231/255, 31/255, 1]
MDFillRoundFlatButton:
text: "This is screen3"
size_hint_y:.06
size_hint_x: .95
pos_hint: {"center_x": 0.5, "center_y": 0.80}
md_bg_color: [147/255, 186/255, 250/255, 1]
font_size: 20
text_color: [255/255, 255/255, 0/255, 1]
'''
class CK1(Screen):
pass
class CK2(Screen):
pass
class CK3(Screen):
pass
sm = ScreenManager()
sm.add_widget(CK1(name='C1'))
sm.add_widget(CK2(name='C2'))
sm.add_widget(CK3(name='C3'))
class Myscreen(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.bind(on_keyboard=self.events)
def build(self):
return Builder.load_string(KV)
def events(self, instance, keyboard, keycode, text, modifiers):
if keyboard == 27:
if self.root.current == "C2":
self.root.current = "C1"
self.root.transition.direction = 'right'
return True
elif self.root.current == "C3":
self.root.current = "C2"
self.root.transition.direction = 'right'
return True
else:
return False
Myscreen().run()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|