Android Question Opening a notification closes activity

Melghost

Member
Licensed User
Longtime User
Hi. I don't know what I'm doing wrong.

I'm trying to show a notification from my app and want to return to my app when I close the notification, but the app is closed instead.

Here's my code:



Main:

B4X:
Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("Layout1")

End Sub



Sub Button1_Click
    Dim NB As NotificationBuilder
    NB.Initialize
    NB.AutoCancel=True
    NB.ContentText="Prueba"
    NB.ContentTitle="Prueba"
    NB.Tag="Hola"
    NB.DefaultSound=True
    NB.DefaultVibrate=True
    NB.DefaultLight=False
    NB.setCustomLight(Colors.ARGB(255,255,0,255),800,400)
    NB.setActivity(Notificacion)
    NB.SmallIcon="icon"
    NB.Notify(0)
End Sub



Notification:

B4X:
Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("LayNotif")                'Nothing special on this layout: Just a label

End Sub

All hidden subs (Resume, Pause...) contain no relevant code (they're void in this example)


My log shows:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true ** (Now is when I open the notification)
** Activity (notification) Create, isFirst = true **
** Activity (notification) Resume **
** Activity (notification) Pause, UserClosed = true **

I thought a new ** Activity (main) Resume ** should appear at the end, and the main activity should be shown again, but obviously I'm mistaken. How can I do it happen?

Thank you


NOTE: In my actual app (not in this example) I actually don't know what activity is open, as the notification is called from a service, so I want to return from the notification to the last open activity
 

DonManfred

Expert
Licensed User
Longtime User
I thought a new ** Activity (main) Resume ** should appear at the end, and the main activity should be shown again, but obviously I'm mistaken. How can I do it happen?
set main as the receiver for this notification i gues...

Why should main come up when you set the activity notification as the receiver?

in your case i would try to use a startactivity(main) in you notification resume
 
Upvote 0

Melghost

Member
Licensed User
Longtime User
Thanks DonManfred. I've found this: NB.setParentActivity(Activity as Object)

Now I'm doing like this:

B4X:
'On every activity from my project
Sub Activity_Resume
    Srv.ActivityOrigen=Me               'Srv is a service, and ActivityOrigen is an object variable
End Sub

B4X:
    Notif1.Initialize

    'New line added to the service which starts the notification
    If ActivityOrigen<>Null Then Notif1.setParentActivity(ActivityOrigen)

    'Next lines as they were before
    Notif1.AutoCancel=True
    Notif1.ContentText="Del equipo de recuento, para " & Destinatarios(Dest)
    Notif1.ContentTitle="Mensaje de Podemos"
    Notif1.Tag=Nombre
    Notif1.DefaultSound=True
    Notif1.DefaultVibrate=True
    Notif1.DefaultLight=False
    Notif1.setCustomLight(Colors.ARGB(255,255,0,255),800,400)
    Notif1.setActivity(EscruMsg)
    Notif1.SmallIcon="icon"
    Notif1.Notify(Dest)

Now the notification returns to the activity that was open when the service was called.
However, after this, when I press the back button, the app finishes instead of starting the previously open activity.

This is what I want to say:

Main activity schedules the service.
User presses a button and Main activity starts Activity2
User presses a button on Activity2, and it starts Activity3
Now the service starts; it shows the notification
The user opens the notification. Then presses back button and Activity3 is shown (all is perfect at the moment)
The user wants to go back to Activity2 and presses the back button, but the app finishes instead.

So it seems that opening a notification makes a return stack get lost, or something similar

I could write StartActivity( (PreviousActivity) ) on every activity Sub Pause, but I think I'm writing more than needed because there's something I ignore.

Any ideas?

Thank you very much
 
Upvote 0

Melghost

Member
Licensed User
Longtime User
Hello.
This is my solution, until someone show a better way to do it:


Main:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")

    'Starts the service to show the notification
    StartServiceAt(Serv,DateTime.Now+10*DateTime.TicksPerSecond,False)

End Sub

Sub Activity_Resume
    If Serv.ActivityNumber>1 Then
        StartActivity(Activity2)
    Else
        Serv.ActivityNumber=1
    End If
End Sub

'I can't write this in Sub Pause because opening the notification also makes UserClosed=True
'     so there's no way to know why the activity is pausing
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode=KeyCodes.KEYCODE_BACK Then
        Serv.ActivityNumber=0
    End If
    Return (False)
End Sub

'Opens Activity2
Sub Button1_Click
    StartActivity(Activity2)
End Sub


Activity2:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout2")
End Sub

'All activities this way:
Sub Activity_Resume
    If Serv.ActivityNumber>2 Then
    '    StartActivity(Activity3)
    Else
        Serv.ActivityNumber=2
    End If
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode=KeyCodes.KEYCODE_BACK Then
        Serv.ActivityNumber=1
    End If
    Return (False)
End Sub


Service:
B4X:
Sub Process_Globals
    Public ActivityNumber As Int
        '0=App is closed
        '1=Main
        '2=Activity2
End Sub

Sub Service_Start (StartingIntent As Intent)
    ShowNotification
End Sub


'Notice that I don't use NB.setParentActivity
Sub ShowNotification
    Dim NB As NotificationBuilder   
    NB.Initialize
    NB.AutoCancel=True
    NB.ContentText="Prueba"
    NB.ContentTitle="Prueba"
    NB.Tag="Hola"
    NB.DefaultSound=True
    NB.DefaultVibrate=True
    NB.DefaultLight=False
    NB.setCustomLight(Colors.ARGB(255,255,0,255),800,400)
    NB.setActivity(Notificacion)
    NB.SmallIcon="icon"
    NB.Notify(0)
End Sub

Notification:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("LayNotif")
End Sub

Sub Activity_Resume
    Log("ActivityNumber=" & Serv.ActivityNumber)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        StartServiceAt(Serv,DateTime.Now + 10* DateTime.TicksPerSecond,False)
        If Serv.ActivityNumber>0 Then StartActivity(Main)      'This line instead NB.setParentActivity(Main)
              'The other way, if I open another activity after the service were started, the notification would return to the previous one
    End If   
End Sub



Well, I think doing this way is quite a botch, but I don't know the right way to do it.

Any suggestions?

Thank you very much
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…