back button functionality on android with kivy

android, kivy, python-2.7

Solution

i guess that i have solved it but should thank both @inclement and @qua-non! your answers guys led me to the right way! so in kv i assume that i gave an id to my screen manager (please refer to my question where i have written the kv code) , in python i should do the following:

    from kivy.core.window import Window
    from kivy.properties import ObjectProperty

    class MyAppClass(FloatLayout):#its a FloatLayout in my case
        _screen_manager=ObjectProperty(None)
        def __init__(self,**kwargs):
            super(MyAppClass,self).__init__(**kwargs)
            #code goes here and add:
            Window.bind(on_keyboard=self.Android_back_click)

        def Android_back_click(self,window,key,*largs):
            if key == 27:
                self._scree_manager.current='screen1'#you can create a method here to cache in a list the number of screens and then pop the last visited screen.
                return True

    class MyApp(App):
        def build(self):
            return MyAppClass()

    if __name__=='__main__':
        MyApp().run()

Problem

in Kivy, when I press the back button on my android device it throws me out of the application. is there a way to return back to the previous screen using the Kivy language and not python? this is what I have written in kivy: ``` <MyAppClass>: AnchorLayout: anchor_x : 'center' anchor_y : 'top' ScreenManager: size_hint : 1, .9 id: _screen_manager Screen: name:'screen1' StackLayout: # irrelevant code Screen: name:'screen2' StackLayout: # irrelevant code ``` I need to manipulate the screen manager and its screens from python... if I can do so I will be ok with python.

Original source