Back button functionality on android

2,422 views
Skip to first unread message

Brian Seel

unread,
Mar 24, 2012, 4:08:13 PM3/24/12
to kivy-...@googlegroups.com
I have been playing with kivy for a few months now to try and build a little app (probably would be easier with the Android API... but I love python... so whatever). I was playing around with Presemt and noticed that the hardware back button only backs completely out of the application. Is there anyway to build functionality into an app so that it will back out by screen like the back button more traditionally does?

I apologize in advance if that is a stupid question... I am learning python and kivy at the same time...

Brian

deakblue

unread,
Mar 25, 2012, 12:49:18 AM3/25/12
to Kivy users support
Hi Brian,

You can trap the android-specific button presses. I have code like
this in my app:

#inside your on_start event, or somewhere else that triggers a
#little later (don't quite remember why this was necessary)

self.bind(on_start = self.post_build_init)


def post_build_init(self,ev):

if platform() == 'android':
import android
import pygame

android.map_key(android.KEYCODE_MENU, 1000)
android.map_key(android.KEYCODE_BACK, 1001)
android.map_key(android.KEYCODE_HOME, 1002)
android.map_key(android.KEYCODE_SEARCH, 1003)

win = self._app_window
win.bind(on_keyboard=self._key_handler)


Then just use your _key_handler (takes 5 args that include the
keycodes) to define your back button behavior.

For reference, take a look at the kivy.uix.settings for how the menu
opens the settings screen, I think that's where I went.

krister viirsaar

unread,
Jun 25, 2013, 4:44:40 AM6/25/13
to kivy-...@googlegroups.com, brian...@gmail.com
to connect topics (google only points to this one) https://groups.google.com/forum/#!topic/kivy-users/otb0o27J7_A

Uxmar

unread,
Oct 6, 2013, 1:27:04 PM10/6/13
to kivy-...@googlegroups.com
Hi, deakblue 

I have days trying to operate the button to go back to android.

I see you with the solution you gave!

But I do not know where in the code place your piece of code, you could help me please.

I really appreciate it your help.  Thanks.

krister viirsaar

unread,
Oct 6, 2013, 4:03:30 PM10/6/13
to kivy-...@googlegroups.com
this is how my app works:

from kivy.core.window import Window

class StoryTreeApp(App):
    use_kivy_settings
= BooleanProperty(False)

   
def build(self):
       
# Irrelevant code
       
Window.bind(on_keyboard=self.hook_keyboard)
       
return root

   
def hook_keyboard(self, window, key, *largs):
       
if key == 27: # BACK
       
# Irrelevant code
       
elif key in (282, 319): # SETTINGS
       
# Irrelevant code


Uxmar

unread,
Oct 16, 2013, 4:28:51 PM10/16/13
to kivy-...@googlegroups.com
Hi krister viirsaar ,

Thank you very much for answering. I was sick so had not responded.

I am new to kivy. I put in my code a piece of code like this:

class TrainccsApp(App):

    def __init__(self):
        super(TrainccsApp, self).__init__()

    def build(self):
        sm = ScreenManager()
        sm.add_widget(MainWindow(name='mainwindow'))
        sm.add_widget(StandardWidgets(name='inputstation'))
        
        Window.bind(on_keyboard=self.hook_keyboard)

        return sm

    def hook_keyboard(self, window, key, *largs):
        if key == 27: # BACK
        # Irrelevant code
            pass
        elif key in (282, 319): # SETTINGS
        # Irrelevant code
            pass

TrainccsApp().run()

But it does not work, do not know how to structure your code in mine work for me, sorry to bother you, but I really need this, you could help me?

Thank you very much in advance for your valuable help

Gabriel Pettier

unread,
Oct 16, 2013, 6:43:54 PM10/16/13
to kivy-...@googlegroups.com
You need to return True if you want the handler to prevent closing the
kivy app.

Cheers
> escribi�:
> >
> > this is how my app works:
> >
> > from kivy.core.window import Window
> >
> > class StoryTreeApp(App):
> > use_kivy_settings = BooleanProperty(False)
> >
> > def build(self):
> > # Irrelevant code
> > Window.bind(on_keyboard=self.hook_keyboard)
> > return root
> >
> > def hook_keyboard(self, window, key, *largs):
> > if key == 27: # BACK
> > # Irrelevant code
> > elif key in (282, 319): # SETTINGS
> > # Irrelevant code
> >
> >
> >
>
> --
> You received this message because you are subscribed to the Google Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Uxmar

unread,
Oct 16, 2013, 7:03:04 PM10/16/13
to kivy-...@googlegroups.com
Hi tshirtman,

I added return True so:

    def hook_keyboard(self, window, key, *largs):
        if key == 27: # BACK
            return True
        # Irrelevant code
            pass
        elif key in (282, 319): # SETTINGS
        # Irrelevant code
            pass

and the application does not close.

But, I need operate the button  go back to android with kivy.

That is, when I press the button to "go back" android native my application return to the previous screen 

You know how it is tshirtman?

Thank you very much for your answer

> escribi�:

Gabriel Pettier

unread,
Oct 16, 2013, 8:02:39 PM10/16/13
to kivy-...@googlegroups.com
Well, before returning True, change the `current` property of your
screen manager to be the right one, you'll want to maintain an history
of the visited screen, in a list, and pop the last one to put it back in
`current` when the user hit the back button.

On Wed, Oct 16, 2013 at 04:03:04PM -0700, Uxmar wrote:
> Hi tshirtman,
> *
> *
> I added return True so:
>
> def hook_keyboard(self, window, key, *largs):
> if key == 27: # BACK
> return True
> # Irrelevant code
> pass
> elif key in (282, 319): # SETTINGS
> # Irrelevant code
> pass
>
> and the application does not close.
>
> But, I need operate the button go back to android with kivy.
>
> That is, when I press the button to "go back" android native my application
> return to the previous screen
>
> You know how it is tshirtman?
>
> Thank you very much for your answer
>
>
> El mi�rcoles, 16 de octubre de 2013 18:13:54 UTC-4:30, tshirtman escribi�:
> > > escribi�:
> > > >
> > > > this is how my app works:
> > > >
> > > > from kivy.core.window import Window
> > > >
> > > > class StoryTreeApp(App):
> > > > use_kivy_settings = BooleanProperty(False)
> > > >
> > > > def build(self):
> > > > # Irrelevant code
> > > > Window.bind(on_keyboard=self.hook_keyboard)
> > > > return root
> > > >
> > > > def hook_keyboard(self, window, key, *largs):
> > > > if key == 27: # BACK
> > > > # Irrelevant code
> > > > elif key in (282, 319): # SETTINGS
> > > > # Irrelevant code
> > > >
> > > >
> > > >
> > >
> > > --
> > > You received this message because you are subscribed to the Google
> > Groups "Kivy users support" group.
> > > To unsubscribe from this group and stop receiving emails from it, send
> > an email to kivy-users+...@googlegroups.com <javascript:>.

Uxmar

unread,
Oct 16, 2013, 8:24:20 PM10/16/13
to kivy-...@googlegroups.com
ok, I have no idea how you do what you suggest, but I will try it and investigate and tell you.

Know of someone who has something in advance?

Thank you for your valuable help.

El miércoles, 16 de octubre de 2013 19:32:39 UTC-4:30, tshirtman escribió:
Well, before returning True, change the `current` property of your
screen manager to be the right one, you'll want to maintain an history
of the visited screen, in a list, and pop the last one to put it back in
`current` when the user hit the back button.

On Wed, Oct 16, 2013 at 04:03:04PM -0700, Uxmar wrote:
> Hi tshirtman,
> *
> *
> I added return True so:
>
>     def hook_keyboard(self, window, key, *largs):
>         if key == 27: # BACK
>             return True
>         # Irrelevant code
>             pass
>         elif key in (282, 319): # SETTINGS
>         # Irrelevant code
>             pass
>
> and the application does not close.
>
> But, I need operate the button  go back to android with kivy.
>
> That is, when I press the button to "go back" android native my application
> return to the previous screen
>
> You know how it is tshirtman?
>
> Thank you very much for your answer
>
>
> El mi�rcoles, 16 de octubre de 2013 18:13:54 UTC-4:30, tshirtman escribi�:
> > > escribi�:

krister viirsaar

unread,
Oct 17, 2013, 3:47:35 AM10/17/13
to kivy-...@googlegroups.com
what tshirtman is saying:

screen = [list of screens that user has already navigated]
if key == 27: # BACK
    change to screen[-1]
    return True

toufic zaarour

unread,
Nov 20, 2013, 3:56:52 AM11/20/13
to kivy-...@googlegroups.com, brian...@gmail.com
Dear krister viirsaar and tshirtman
it is fairly easy to do what you suggest with python (setting the return button and appending to a list the last screens visited by the user) but how to return to the previous screen when the screen manager and the screens are written with the kivy language? ex:

Enter code here...
<MyAppClass>:
    AnchorLayout:
        anchor_x : 'center'
        anchor_y : 'top'
        ScreenManager:
            size_hint : 1, .9
            id: _screen_manager
           
            Screen:
                name:'screen1'
                StackLayout:
                    # irrelevent code

            Screen:
                name:'screen2'
                StackLayout:
                    # irrelevent code

ZenCODE

unread,
Nov 20, 2013, 1:05:23 PM11/20/13
to kivy-...@googlegroups.com, brian...@gmail.com
Try looking at the "Referencing Widgets" section here: http://kivy.org/docs/guide/lang.html

You just will create an ObjectProperty which will bind to your ScreenManager in the KV file. Try it, and if you don't get it right, post your efforts and we will help ;-)

Cheers

toufic zaarour

unread,
Nov 21, 2013, 6:53:43 AM11/21/13
to kivy-...@googlegroups.com, brian...@gmail.com

    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()


in kivy:


<MyAppClass>:
   
AnchorLayout:
        anchor_x
: 'center'
        anchor_y
: 'top'
       
ScreenManager:
            size_hint
: 1, .9
            id
: _screen_manager
           
           
Screen:
                name
:'screen1'
               
StackLayout:
                   
# irrelevent code

           
Screen:
                name
:'screen2'
               
StackLayout:
                   
# irrelevent code

ZenCODE

unread,
Nov 22, 2013, 4:53:05 PM11/22/13
to kivy-...@googlegroups.com, brian...@gmail.com
Okay... So does this work? Does it not? Some information would be useful. Offhand, would suggest adding some binding to your kv file.

<MyAppClass>:
    _screen_manager: _screen_manager

    AnchorLayout:

But it's difficult to say without knowing what's happening on your side?

Cheers

toufic zaarour

unread,
Nov 24, 2013, 1:12:07 PM11/24/13
to kivy-...@googlegroups.com
It works just fine.

Dmytro Kravchenko

unread,
Mar 18, 2021, 7:22:45 AM3/18/21
to Kivy users support
In me work fine this example. Maybe somebody help, i don't know =)

воскресенье, 24 ноября 2013 г. в 19:12:07 UTC+1, touf...@gmail.com:
It works just fine.
Reply all
Reply to author
Forward
0 new messages