Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kivy Android app running in background crashes when intent tries to pull it to top #1271

Closed
ghost opened this issue May 13, 2018 · 10 comments
Closed

Comments

@ghost
Copy link

ghost commented May 13, 2018

@EmmaCaunter commented on May 4, 2018, 6:17 PM UTC:

Versions

  • Python: 2.7
  • OS: Android
  • Kivy: master
  • Kivy installation method: Git

Description

When the app is in the background, I want to use Android's Intent to bring app back to top. When the intent activates, the app instantly crashes and then restarts. This issue occurs with the new toolchain but not with the old one.

Code and Logs

Code to replicate:

***************

from kivy.app import App
from kivy.uix.button import Button
from android.broadcast import BroadcastReceiver
from jnius import autoclass
from plyer.platforms.android import SDK_INT

PythonActivity = autoclass('org.kivy.android.PythonActivity')
NotificationBuilder = autoclass('android.app.Notification$Builder')
Context = autoclass('android.content.Context')
Intent = autoclass('android.content.Intent')
PendingIntent = autoclass('android.app.PendingIntent')
Uri = autoclass('android.net.Uri')
Notification = autoclass('android.app.Notification')
AndroidString = autoclass('java.lang.String')

class TestApp(App):
    def build(self):

        return Button(text='Hello World')

    def on_resume(self):
        print "On resume"

    def on_pause(self):
        print "on pause"

        activity = PythonActivity.mActivity
        Drawable = autoclass("{}.R$drawable".format(activity.getPackageName()))

        notification_service = activity.getSystemService(Context.NOTIFICATION_SERVICE)

        icon = getattr(Drawable, 'icon')
        noti = NotificationBuilder(activity)

        noti.setPriority(Notification.PRIORITY_MAX)
        noti.setDefaults(Notification.DEFAULT_ALL)  

        noti.setContentTitle(AndroidString(("title").encode('utf-8')))
        noti.setContentText(AndroidString(("message").encode('utf-8')))
        noti.setSmallIcon(icon)
        id = 22701

        app_intent = Intent(Intent.ACTION_PROVIDER_CHANGED)
        pending_intent = PendingIntent.getBroadcast(activity, id, app_intent, PendingIntent.FLAG_UPDATE_CURRENT)
        app_intent.setData(Uri.parse('close'))

        if not hasattr(self, 'br'):
             self.broadcast_receiver = BroadcastReceiver(
                 self.on_broadcast, actions=['provider_changed', 'run'])
             self.broadcast_receiver.start()

        noti.addAction(0, AndroidString("Start"), pending_intent)
        if SDK_INT >= 16:
            noti = noti.build()
        else:
            noti = noti.getNotification()
        notification_service.notify(0, noti)
        return True

    def on_broadcast(self, context, intent):
        print "On Broadcast"
        activity = PythonActivity.mActivity
        notification_service = activity.getSystemService(Context.NOTIFICATION_SERVICE)
        notification_service.cancel(0)
        intent = Intent(activity.getApplicationContext(), PythonActivity)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
        activity.getApplicationContext().startActivity(intent)

TestApp().run()

**************

Put app into background, which triggers notification. Touch notification to re-open app, and the app crashes with the following error:

A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4 in tid 7199 (SDLThread)

More detailed logging reveals the following SIGSEGV fingerprint:

04-30 15:53:41.350 3138-3269/? I/bt-btm: BTM_CheckAdvData type=0x19
04-30 15:53:41.350 3138-3269/? I/bt-btm: BTM_CheckAdvData type=0x03
04-30 15:53:41.350 3138-3269/? D/bt-btm: BR/EDR NOT support bit not set, treat as DUMO
04-30 15:53:41.350 458-458/? I/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
04-30 15:53:41.350 458-458/? I/DEBUG: Build fingerprint: 'htc/bm/htc_m8:5.0.1/LRX22C/463267.2:user/release-keys'
04-30 15:53:41.350 458-458/? I/DEBUG: Revision: '0'
04-30 15:53:41.350 458-458/? I/DEBUG: ABI: 'arm'
04-30 15:53:41.350 458-458/? I/DEBUG: pid: 9964, tid: 10051, name: SDLThread  >>> com.test.example <<<
04-30 15:53:41.360 458-458/? I/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x4
04-30 15:53:41.360 458-458/? I/DEBUG:     r0 a0e3e108  r1 a0e3e108  r2 fffffc7c  r3 00000000
04-30 15:53:41.360 458-458/? I/DEBUG:     r4 00000001  r5 97eb9ae8  r6 9dd4ee40  r7 00000001
04-30 15:53:41.360 458-458/? I/DEBUG:     r8 00000001  r9 97eb9ae8  sl 00000001  fp b3e620c0
04-30 15:53:41.360 458-458/? I/DEBUG:     ip a0e5fb00  sp 97eb5810  lr a0da36f9  pc a0da31dc  cpsr 600b0030
04-30 15:53:41.370 458-458/? I/DEBUG: backtrace:
04-30 15:53:41.370 458-458/? I/DEBUG:     #00 pc 000c61dc  /data/app/com.test.example-1/lib/arm/libpython2.7.so (PySys_GetObject+11)
04-30 15:53:41.370 458-458/? I/DEBUG:     #01 pc 000c66f5  /data/app/com.test.example-1/lib/arm/libpython2.7.so (PySys_SetArgvEx+84)
04-30 15:53:41.440 3138-3269/? I/bt-hci: BLE HCI(id=62) event = 0x02)
04-30 15:53:41.440 3138-3269/? I/bt-hci: btu_ble_process_adv_pkt
04-30 15:53:41.440 3138-3269/? D/bt-btm: btm_ble_process_adv_pkt:bda= 28:39:5e:4f:c9:b7

This issue was moved by tshirtman from kivy/kivy/issues/5728.

@ghost
Copy link
Author

ghost commented May 13, 2018

welcome[bot] commented on May 4, 2018, 6:17 PM UTC:

👋 Thanks for opening your first issue here! Be sure to follow the issue template!

@ghost
Copy link
Author

ghost commented May 13, 2018

@EmmaCaunter commented on May 5, 2018, 8:16 PM UTC:

A work-around for this issue is to add android:launchMode="singleTask" or android:launchMode="singleTop" to the AndroidManifest.tmpl.xml. Setting the android.manifest.launch_mode variable in the buildspec does not work - the variable is not copied into the generated AndroidManifest.xml.

@brvier
Copy link
Contributor

brvier commented Jun 5, 2018

I ve a similar issue with URL Intent, and using android:launchMode="singleTask" or android:launchMode="singleTop" didn't solve the problem.

Edit : i added some logs. Crash occurs in https://github.com/kivy/python-for-android/blob/master/pythonforandroid/bootstraps/sdl2/build/jni/src/start.c

PySys_SetArgv(argc, argv);

06-05 09:48:29.742 27304 27357 F libc : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4 in tid 27357 (SDLThread)

@inclement
Copy link
Member

inclement commented Jan 31, 2019

Does anyone have an example app demonstrating this issue? It would be convenient to not reinvent a way to test it.

Also, does it still happen with the new SDL2.0.9 upgrade?

Edit: @AndreMiras pointed out I misunderstood the test above, it should be enough

@brvier
Copy link
Contributor

brvier commented Jan 31, 2019

Previous example isn t enough ?

@inclement
Copy link
Member

Looks like this is fixed by adding android:launchMode="singleInstance" (you can use --activity-launch-mode=singleInstance). I'm looking to fully understand it, but it looks like we can fix it easily if that has no problematic effects.

@inclement
Copy link
Member

Verified that singleTask also works. @brvier I saw that you tried this before, but maybe the behaviour has been fixed by the recent update to SDL2.0.9.

@inclement
Copy link
Member

Reading about it, I'm seeing a lot of stackoverflow answers etc. suggesting that the single activity stuff shouldn't really be used. I'm not clear if this is true, or the the kind of thoughtless misinformation you see sometimes from people who have only considered how normal java apps will work.

It looks like the old toolchain used singleTask. I'm going to make a PR for this, it looks like it's correct and fine.

@inclement
Copy link
Member

Thanks @EmmaCaunter for the clear example, it would have been much harder to debug without it.

@brvier
Copy link
Contributor

brvier commented Mar 13, 2019

Verified that singleTask also works. @brvier I saw that you tried this before, but maybe the behaviour has been fixed by the recent update to SDL2.0.9.

Sorry to not have answer before, indeed look like fixed with various SDL2.0.9 or master..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants