Android Question Activity.Finish

Status
Not open for further replies.

Bucky108

Member
Licensed User
Longtime User
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.

Any suggestions?
 

Jorge M A

Well-Known Member
Licensed User
Longtime User
Have you try to put your sub within a service module?
As far as I know, is the OS who decide when to kill the process of the activity.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Upvote 0

Bucky108

Member
Licensed User
Longtime User
Jorge - I can't put the sub in the service module because I want to have a shortcut icon as part of the app.

Peter - Is that post saying that if you are sure that this is the only remaining activity then it's OK to use ExitApplication?
 
Upvote 0

Bucky108

Member
Licensed User
Longtime User
For example:
B4X:
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.
 
Upvote 0

Bucky108

Member
Licensed User
Longtime User
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.
 
Upvote 0

Bucky108

Member
Licensed User
Longtime User
Erel,

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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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

This will prevent the app from appearing in the recent apps list.
Source: https://stackoverflow.com/questions/27633329/activity-excludefromrecents-not-working-on-android-5-0

I've only tested it on Android 8 device.
 
Upvote 0

Bucky108

Member
Licensed User
Longtime User
It worked! I also only tested it on an Android 8 device. It only goes though the code if SdkVersion >= 25 so I think it will work on 7.1 and above.

Thanks Erel.
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
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

This will prevent the app from appearing in the recent apps list.
Source: https://stackoverflow.com/questions/27633329/activity-excludefromrecents-not-working-on-android-5-0

I've only tested it on Android 8 device.


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
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top