Android Question How to kill all activities in the app?

Discussion in 'Android Questions' started by Beja, Mar 4, 2015.

  1. Beja

    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.
     
  2. timwil

    timwil Active Member Licensed User

    I end my apps with

    Activity.Finish
    ExitApplication
     
  3. Beja

    Beja Expert Licensed User

    In which activity you put that if you have 4 beside main, and you want to exit while in the third?
     
  4. timwil

    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)
     
  5. Beja

    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...
     
  6. KZero

    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
    Code:
    SetApplicationAttribute(android:clearTaskOnLaunch, "true")
    this approach makes your app consume more resources its much better to use multiple activities instead
     
    scsjc and Beja like this.
  7. timwil

    timwil Active Member Licensed User

  8. Beja

    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?
     
  9. Troberg

    Troberg Well-Known Member Licensed User

    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.
     
  10. KZero

    KZero Active Member Licensed User

    Code:
    '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
     
  11. Beja

    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
     
  12. KZero

    KZero Active Member Licensed User

    -Add this sub to "Code Module"
    -in the Code Module Process_Globals add
    Code:
    Dim aExit as boolean
    -in your exit buttons add
    Code:
    CodeModule.aExit=True
    -in all activities Resume event add
    Code:
    if CodeModule.aExit=True then
         CodeModule.RestartApp
         
    Return
    End if
     
    Last edited: Mar 5, 2015
  13. Beja

    Beja Expert Licensed User

    Code:
    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).
     
  14. KZero

    KZero Active Member Licensed User

    the user will exit the app normally using any exit button from any activity using
    Code:
    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
    Code:
    '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
     
  15. Beja

    Beja Expert Licensed User

    thanks buddy, will try that
    ya rab, insha'alla teshtaghil
     
    KZero likes this.
  16. Beja

    Beja Expert Licensed User

    Code:
    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 :)
     
  17. KZero

    KZero Active Member 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
     
  18. Beja

    Beja Expert 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.
     
  19. KZero

    KZero Active Member Licensed User

    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
     
  20. Beja

    Beja Expert Licensed User

    Alright! thanks for the detailed explanation.. :)
     
    KZero likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice