I know this is been hashed and rehashed many times on the forums but I still haven't found a way to really close an activity. I've tried of course Activity.Finish and also:
B4X:
Sub CloseActivities
Dim jo As JavaObject
jo.InitializeContext
jo.RunMethod("finishAffinity", Null)
End Sub
Nothing seems to close the activity. I've tried waiting literally overnight and the activity still shows in recent apps.
What I'm trying to do is execute a sub from a shortcut from the app icon and not show the activity.
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim b As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
b.Initialize("b")
Activity.AddView(b,100dip,100dip,100dip,100dip)
End Sub
Sub b_Click
Activity.Finish
End Sub
Click the button, then click the "overview" button on the bottom of the screen and the activity is still displayed.
I'm using shortcuts for the app to run a specific process and then exit. When the user selects a shortcut, I want the user to not see the activity screen at all even in the recent apps list.
I'm using DonManfred's "ShortcutHelper" library to add the shortcuts.
The activity is started normally in the Main module (i.e. I'm not using StartActivity).
I do a check to see if a shortcut was selected and if yes, bypass Activity.LoadLayout, run the sub for the requested function, and then I want to exit the app completely.
You are confusing two different things. You are not talking about exiting the app completely but rather about preventing the activity from appearing in the recent apps list.
Add this code to your activity:
B4X:
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim ActivityManager As JavaObject = ctxt.RunMethod("getSystemService", Array("activity"))
Dim tasks As List = ActivityManager.RunMethod("getAppTasks", Null)
If tasks.IsInitialized And tasks.Size > 0 Then
Dim task As JavaObject = tasks.Get(0)
task.RunMethod("setExcludeFromRecents", Array(True))
End If
You are confusing two different things. You are not talking about exiting the app completely but rather about preventing the activity from appearing in the recent apps list.
Add this code to your activity:
B4X:
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim ActivityManager As JavaObject = ctxt.RunMethod("getSystemService", Array("activity"))
Dim tasks As List = ActivityManager.RunMethod("getAppTasks", Null)
If tasks.IsInitialized And tasks.Size > 0 Then
Dim task As JavaObject = tasks.Get(0)
task.RunMethod("setExcludeFromRecents", Array(True))
End If
Hi Sorry to back a old thread, but the subject of my question is totally linked to this.
this code is working fine on Android 5+, perfect....
I would like to know if anyone has this same solution for Android <5.
i need excludefromrecents via code (not manifest), and this need work on android 3, 4 etc...
I tried some tips found in the forum, using intent etc ... but neither one worked correctly the same as the code above.
thx
Edit:
I made this. i dont know if is the right way to make this, but its working .
here is the code
B4X:
Sub Activity_Create(FirstTime As Boolean)
If Activity.GetStartingIntent.HasExtra("exitapp") Then ExitApplication
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Activity_Click
Private jo As JavaObject : jo.InitializeContext : jo.RunMethod("Remover_Recentes", Null)
End Sub
#If JAVA
public void Remover_Recentes()
{
import android.content.Intent;
Intent intent = new Intent(this, main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.putExtra("exitapp", true);
startActivity(intent);
return;
}
#end if