Android Question [Solved] Start new activity from the main activity only if the user press the notification

asales

Expert
Licensed User
Longtime User
My sample is in attached. I have 2 activities in the sample: Main and Tips.
The button in the main activity close the app and start a notification.

I want to do this:
If the user presses on the notification, open the Main activity and show the Tips activity (it's OK).
The Tips activity is started only if the user press the notification.

If him start the app from tap in the icon or start from the recents apps list, the app do NOT show the Tips activity.

I tried several codes from the forum, but my code don't works.
If I restart the app from the recents apps list, the Tips activity is started again.

How can I fix it?

Thanks in advance for any tips.

B4X:
Sub Globals
    Dim in As Intent
End Sub

Sub Activity_Create(FirstTime As Boolean)
    in = Activity.GetStartingIntent

    If (in <> Starter.GlobalIntent) Then 
        Starter.GlobalIntent = in       

        'Start the tips activity only the app start from click in notification
        If in.HasExtra("Notification_Tag") And (in.GetExtra("Notification_Tag") = "odNotify") Then
            StartActivity(tips)  'I do not want to open the Tips activity if the app is opens through the list of recent apps
        End If
    End If

    Activity.LoadLayout("layMain")
End Sub

Sub Button1_Click
    StartNotification
    Activity.Finish
End Sub

Sub StartNotification
    Dim n As Notification
    n.Initialize2(n.IMPORTANCE_HIGH)
    n.Icon = "icon"
    n.AutoCancel = True
    n.Vibrate = False
    n.Sound = True
    n.SetInfo2("Hello", "Tip of day", "odNotify", Me)
    n.Notify(1)
End Sub
 

Attachments

  • notification1.zip
    11 KB · Views: 255

asales

Expert
Licensed User
Longtime User
Seems to work fine here. The tips activity is only started when I click on the notification.[/code]
Yes, but if I close the app and start it again (from the recent apps list) the tips activity is show again, and I don't want it (in this case I want to show only the main activity).

Why aren't you setting the Tips activity to be the notification target?
It is better to make the test in Activity_Resume.
Thanks for your tips.
I tried this, but If I restart the app from the recent list, it will show the tips activity and my goal is start with the main activity and show the tips activity only if I start it from the notification (not from the tap in icon or from the recent apps list).
I base my code on this:
https://www.b4x.com/android/forum/t...intent-be-reset-or-cleared.38004/#post-224814
B4X:
n.SetInfo2("Hello", "Tip of day", "odNotify", tips)
If I use this code, the tips activity is show (and not the main activity).
If I make this:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        StartActivity(Main)
    End If
End Sub
the main activity is show after close the tips activity, but if I close the app and restart from the recent list, the tips activity is show first (and not the main activity).
 
Last edited:
Upvote 0

asales

Expert
Licensed User
Longtime User
If I understand you correctly you want to close the Tips activity when the app is paused. You can add this:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    Activity.Finish
End Sub

If I use this code, the tips activity is closed, but if I restart the app from the recent apps list, the tips activity are show again and I do not want to show the activity again (only if the user click on the notification and not in the recent list apps).

I do not know if it is possible, because if I open the apps from the recent list it is open directly in Activity_Resume.
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I think that I solved it.

I created a variable in the Starter:
B4X:
Dim cancel_tip As boolean = False
When the notification starts the cancel_tip = false.
B4X:
Sub StartNotification
   Starter.cancel_tip = False '<--
   n.Initialize2(n.IMPORTANCE_HIGH)
   (...)
   n.Notify(1)
End Sub

When I open the tips activity, then set cancel_tip = True.

If I reopen the app from the recent list apps, it checks if the cancel_tip and don't show the tips activity again.
B4X:
If in.HasExtra("Notification_Tag") And (in.GetExtra("Notification_Tag") = "odNotify") And (Starter.cancel_tip = False) Then
   StartActivity(tips) 
End If
 
Upvote 0
Top