Android Question Invoking The 'Recent Apps' Screen In Code

RichardN

Well-Known Member
Licensed User
Longtime User
It is possible to invoke the system 'Home' screen from within code by executing:-
B4X:
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "")
    i.AddCategory("android.intent.category.HOME")
    i.Flags = 0x10000000
    StartActivity(i)

Can a similar method be used to invoke the 'Recent Apps' screen ?
 

LucaMs

Expert
Licensed User
Longtime User
Erel ? I found:

B4X:
Intent intent = new Intent ("com.android.systemui.recent.action.TOGGLE_RECENTS");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setComponent(new ComponentName("com.android.systemui", "com.android.systemui.recent.RecentsActivity"));
startActivity (intent);

B4X:
    Private Const FLAG_ACTIVITY_NEW_TASK As Int = 268435456
    Private Const FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS As Int = 8388608

My attempt failed, however:
B4X:
Sub Button1_Click
    Dim Intnt As Intent
    Intnt.Initialize(Intnt.ACTION_MAIN, "")
    Intnt.Flags = Bit.Or(FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
    Intnt.SetComponent("com.android.systemui.recent.RecentsActivity")
    StartActivity(Intnt)
End Sub
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
@LucaMs.... I also saw that example on StackOverflow but, like you, I had no success.

I speak no Java so translating for B4A use can be very hit & miss. This is not the first time I find myself with a similar problem with intents or reflections. Maybe someone could write a primer for those of us that are mono-lingual !
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the equivalent B4A code:
B4X:
Private Sub Button1_Click
    Dim FLAG_ACTIVITY_NEW_TASK As Int = 268435456
    Dim FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS As Int = 8388608
    Dim Intnt As Intent
    Intnt.Initialize("com.android.systemui.recent.action.TOGGLE_RECENTS", "")
    Intnt.Flags = Bit.Or(FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
    Intnt.SetComponent("com.android.systemui/com.android.systemui.recent.RecentsActivity")
    StartActivity(Intnt)
End Sub
It doesn't work on Android 12.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Likewise I do not have an older device to hand but using a Samsung Galaxy S21 Ultra 5G running Android 12 I get the same result.

It throws this exception at StartActivity(i)

B4X:
Private Sub btnRecent_Click
    
    Dim FLAG_ACTIVITY_NEW_TASK As Int = 268435456
    Dim FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS As Int = 8388608
    Dim i As Intent
    i.Initialize("com.android.systemui.recent.action.TOGGLE_RECENTS", "")
    i.Flags = Bit.Or(FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
    i.SetComponent("com.android.systemui/com.android.systemui.recent.RecentsActivity")
    StartActivity(i)
        
End Sub

Logger connected to: samsung SM-G998B
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 59 (Main)
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.systemui/com.android.systemui.recent.RecentsActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2098)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1747)
at android.app.Activity.startActivityForResult(Activity.java:5465)
at android.app.Activity.startActivityForResult(Activity.java:5423)
at android.app.Activity.startActivity(Activity.java:5809)
at android.app.Activity.startActivity(Activity.java:5762)
at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:857)
at com.b4a.example.main._btnrecent_click(main.java:467)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7792)
at android.widget.TextView.performClick(TextView.java:16045)
at android.view.View.performClickInternal(View.java:7769)
at android.view.View.access$3800(View.java:910)
at android.view.View$PerformClick.run(View.java:30184)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8582)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…