Reputation:
I am trying to run this type of code on android but when i tap mobile back button, inspite of coming to the previous screen app is closed, i used this piece of code to stop the App for being closed:
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
Window.bind(on_keyboard=self.BackButton)
def BackButton(self, window, key, *args):
if key==27:
return True
But how to return to the previous screen. and how to use ActionPrevious Button to come at previous screen?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<MyAppGUI>:
orientation:'vertical'
id: box
ActionBar:
ActionView:
ActionPrevious:
title: 'How to go back using this button???'
with_previous: True
ScreenManager:
id: screenmanger
Screen:
name: 's1'
Button:
text: 'goto screen 2'
on_release: screenmanger.current='s2'
Screen:
name: 's2'
Button:
text: 'goto screen 3'
on_release: screenmanger.current='s3'
Screen:
name: 's3'
Button:
text: 'goto main screen'
on_release: screenmanger.current='s1'
""")
class MyAppGUI(BoxLayout):
pass
class MyApp(App):
def build(self):
return MyAppGUI()
if __name__=='__main__':
MyApp().run()
Upvotes: 1
Views: 3652
Reputation: 4513
To return to the previous window using ActionPrevious
button you just need to bind its on_release
event to a function that uses current
property to set the previous screen using the name returned by ScreenManager.previous ()
method or use a list that serves as a history of the windows visited. To use the Back key on Android, your code is correct in principle, at least on my device it works without problems:
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
Builder.load_string("""
<MyAppGUI>:
sm: screenmanger
orientation:'vertical'
id: box
ActionBar:
ActionView:
ActionPrevious:
title: 'How to go back using this button???'
with_previous: True
on_release: root.set_previous_screen()
ScreenManager:
id: screenmanger
Screen:
name: 's1'
Button:
text: 'goto screen 2'
on_release:
screenmanger.transition.direction = 'right'
screenmanger.current='s2'
Screen:
name: 's2'
Button:
text: 'goto screen 3'
on_release:
screenmanger.transition.direction = 'right'
screenmanger.current='s3'
Screen:
name: 's3'
Button:
text: 'goto main screen'
on_release:
screenmanger.transition.direction = 'right'
screenmanger.current='s1'
""")
class MyAppGUI(BoxLayout):
sm = ObjectProperty()
def __init__(self, **kwargs):
super(MyAppGUI, self).__init__(**kwargs)
Window.bind(on_keyboard=self._key_handler)
def _key_handler(self, instance, key, *args):
if key is 27:
self.set_previous_screen()
return True
def set_previous_screen(self):
if self.sm.current != 's1':
self.sm.transition.direction = 'left'
self.sm.current = self.sm.previous()
class MyApp(App):
def build(self):
return MyAppGUI()
if __name__ == '__main__':
MyApp().run()
Upvotes: 1