Android Question How to kill all activities in the app?

Beja

Expert
Licensed User
The tech advice is to let the OS kill them at its own convenience! but the problem is when I run the same app again,
it started from where it last closed, not from Main activity. and there's a lot of data in memory too.
 

timwil

Active Member
Licensed User
I only use the main activity

If you have service modules you can use stopservice(name)

I am trying to understand why have multiple Activities - I just use panels and make them visible or not visible as required or zoom them down to .setlayout(0,0,1,1)
 

Beja

Expert
Licensed User
You are right, if I use only one activity then there is no problem.. the problem is when I use more than activity, and this is very normal in large business apps.
Thanks anyways.
Question still on...
 

KZero

Active Member
Licensed User
if all activities are finished the app should start from the main activity in the next time you run it

adding this line to your manifest editor may help
B4X:
SetApplicationAttribute(android:clearTaskOnLaunch, "true")
I only use the main activity

If you have service modules you can use stopservice(name)

I am trying to understand why have multiple Activities - I just use panels and make them visible or not visible as required or zoom them down to .setlayout(0,0,1,1)
this approach makes your app consume more resources its much better to use multiple activities instead
 

Beja

Expert
Licensed User
Thanks KZero, but the manifest line didn't change it..
If all activities are finished then the app will start from main next time.. but the question is how to finish all the activities?
 

Troberg

Well-Known Member
Licensed User
I only use the main activity

If you have service modules you can use stopservice(name)

I am trying to understand why have multiple Activities - I just use panels and make them visible or not visible as required or zoom them down to .setlayout(0,0,1,1)
You can load them and unload them as needed to conserve resources, and keep as much code as possible in classes. That way, you can get "best of both worlds", you get the simplicity of a single activity without the overhead of a single activity.
 

KZero

Active Member
Licensed User
Thanks KZero, but the manifest line didn't change it..
If all activities are finished then the app will start from main next time.. but the question is how to finish all the activities?
B4X:
'code by JakeBullet70

Sub RestartApp
Dim R, R2 As Reflector
    R.Target = R.GetActivity
    R.Target = R.RunMethod("getApplicationContext")
    R2.Target = R.RunMethod("getPackageManager")
   
Dim I As Intent
    I = R2.RunMethod2("getLaunchIntentForPackage", R.RunMethod("getPackageName"), "java.lang.String" )
    R.Target = I
    R.RunMethod2("addFlags",  67108864, "java.lang.int")
    StartActivity(I)
End Sub
this code will close all activities and restart the app
you can use it in Activity_Resume after checking if the app need to be restarted
 

Beja

Expert
Licensed User
Thanks KZero for your efforts..
(Activity_Resume ) of each activity? I have 5 activities in my project.
Also I don't want to restart the app. but when I restart it (anytime later) must restart from Main
 

KZero

Active Member
Licensed User
Thanks KZero for your efforts..
(Activity_Resume ) of each activity? I have 5 activities in my project.
Also I don't want to restart the app. but when I restart it (anytime later) must restart from Main
-Add this sub to "Code Module"
-in the Code Module Process_Globals add
B4X:
Dim aExit as boolean
-in your exit buttons add
B4X:
CodeModule.aExit=True
-in all activities Resume event add
B4X:
if CodeModule.aExit=True then
     CodeModule.RestartApp
     Return
End if
 
Last edited:

Beja

Expert
Licensed User
B4X:
if CodeModule.aExit=True then CodeModule.RestartApp
Return
Sorry, will this restart the app if aExit is true? but I don't want to restart the app.. (if I correctly understood your code).
 

KZero

Active Member
Licensed User
B4X:
if CodeModule.aExit=True then CodeModule.RestartApp
Return
Sorry, will this restart the app if aExit is true? but I don't want to restart the app.. (if I correctly understood your code).
the user will exit the app normally using any exit button from any activity using
B4X:
Sub btnExit

   CodeModule.aExit=True
   Activity.Finish
End sib
The process will be cached for sometime by the system

now if the user opened the app again the last Activity_Resume Event will be raised and execute this code
B4X:
'put it in all Activties
Sub Activity_Resume
   if CodeModule.aExit=True then
     CodeModule.RestartApp
     Return
   End if
end sub
End if
and now the app will start from the Main activity and all saved data in other activities will be lost
 

Beja

Expert
Licensed User
B4X:
Sub btnExit

   CodeModule.aExit=True
   Activity.Finish
End sib
one more question: Where will this aExit variable live? will it still be alive after the activity is killed? or should I store it somewhere in
text file or SQLite database :)
 

KZero

Active Member
Licensed User
B4X:
Sub btnExit

   CodeModule.aExit=True
   Activity.Finish
End sib
one more question: Where will this aExit variable live? will it still be alive after the activity is killed? or should I store it somewhere in
text file or SQLite database :)
it will be alive if the process still cached in memory, and if not the application will start normally because its already finished and its memory cleared
 

Beja

Expert
Licensed User
it will be alive if the process still cached in memory, and if not the application will start normally because its already finished and its memory cleared
Well, I can pray for the process to live long, but you can't guarantee because on exit you only finished one activity, not all.. and your code depends on the life of the aExit variable.
 

KZero

Active Member
Licensed User
Well, I can pray for the process to live long, but you can't guarantee because on exit you only finished one activity, not all.. and your code depends on the life of the aExit variable.
don't worry about it
aExit variable declared in process_global so it will be alive as long as the app still alive

if aExit not alive it mean your app is already not in the memory and the code is not needed here, the app will start from the main activity normally
 
Top