Search code examples
androidpythonkivybuildozer

Having multiple activities in Kivy for android


As you might be aware of, switching between activities in Android is done using instantiating an Intent and starting it. Something like

Intent i = new Intent(ThisActivity.this, AnotherActivity.class);
startActivity(i);

This goes from ThisActivity to AnotherActivity when the intent is started. If you are in AnotherActivity, pressing 'Back' button of your android device returns you to the previous Activity you have been (ThisActivity). How does one implement such behavior in Kivy?

I use Screen and ScreenManager for showing and navigating between different pages but pressing 'Back' button exits the app. As I have observed in the buildozer log when trying to make apk out of kivy app,

Starting: Intent { act=org.renpy.android.PythonActivity cmp=org.test.community/org.renpy.android.PythonActivity }

It seems that only one Activity named PythonActivity is created. Is it possible to create multiple activities or to have the similar android Activity navigation experience in Kivy?


Solution

  • We do it with the help of pyjnius.The following code illustrates map intent and answers your query.

    from jnius import cast
    from jnius import autoclass
    
    # import the needed Java class
    PythonActivity = autoclass('org.kivy.android.PythonActivity')
    Intent = autoclass('android.content.Intent')
    Uri = autoclass('android.net.Uri')
    
    # create the intent
    mapintent = Intent()
    mapintent.setAction(Intent.ACTION_VIEW)
    msg = "Delhi"
    mapintent.setData(Uri.parse("geo:0,0?q="+msg))
    currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
    currentActivity.startActivity(mapintent)
    

    You will find more examples here