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()
By default, Kivy app closes when the back key is pressed in the andoid. So you need to remove that behaviour first by adding this line in your beginning of your code.
from kivy.core.window import Window
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
You can change the screen or go back to another screen by implementing this code in your screen class
class CK2(Screen):
def on_enter(self):
Window.bind(on_keyboard=self.back_click)
def on_pre_leave(self):
Window.unbind(on_keyboard=self.back_click)
def back_click(self,window, key, keycode, *largs):
if key == 27:
"""Mention here, what your app should do when user clicks back button in screen C2"""
print('Going to screen C1')
Do this for all of your screen classes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With