Android Question Exclude from recents programatically

plager

Member
Hello community,
I am about to make my app being exlude from recents list programatically. Unfortunatelly, can't achieve that in any way.
I obviously do know about tag in manifest:
B4X:
SetActivityAttribute(Main, "android:excludeFromRecents", "true")
However, it doesn't work in runtime mode, in different words - manifest can't be changed in runtime.

I went through forum, found some threads, but none of those works for me:
B4X:
https://www.b4x.com/android/forum/threads/remove-app-from-recent-list-or-reset-intent.32316/
https://www.b4x.com/android/forum/threads/activity-getstartingintent-setactivityresult.19865/
https://www.b4x.com/android/forum/threads/using-startactivityforresult-with-javaobject.40374/

In mine project there is (only) one activity called 'Main'.
So, how can I achieve to change this main acitivity be excluded from recent (or not) according to user choice?
 

plager

Member
After a while i found a solution:
this:
SetActivityAttribute(Main, "android:excludeFromRecents", "true")
must NOT be in manifest.

Modify your Activity_KeyPress procedure:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Dim jo As JavaObject
        jo.InitializeContext
        jo.RunMethod("finishAndRemoveTask", Null)

        Activity.Finish
        Return True
            
    End If

    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

Also modify Activity_Pause:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("finishAndRemoveTask", Null)

    B4XPages.Delegate.Activity_Pause
End Sub

Works fluently on Samsung Galaxy Z Flip 7 FE, Android 16.
 
Upvote 0
Top