Android Tutorial Android Process and activities life cycle

Discussion in 'Tutorials & Examples' started by Erel, Nov 3, 2010.

  1. ilan

    ilan Expert Licensed User

    hi erel, i am loading a layout on firsttime = false

    the code i have posted is only from the main activity but this activity only show the logo then it starts the menu activity where you can see the code in the video and i am loading a layout also on firsttime = false

    if i would not then also by clicking the home button it would not work, it is very strange the by clicking the home button it is ok but back button not
    they do the same thing or am i wrong?

    both will exit the activity and recreate it again by entering the activity again and there should be the same behaver for both process but it does not


    i am finishing the activity on activity.pause because i dont want to enter it again it is only the logo activity and if i would not finish it i would get back to everytime i exit my app but i dont want to show again and again the logo activity, this is the reason i am finishing it
  2. Erel

    Erel Administrator Staff Member Licensed User

    Go over your code again. It's simply wrong. You are not starting the timer when FirstTime is False so the second activity never starts.

    Sorry but I don't see anything strange here.
  3. ilan

    ilan Expert Licensed User

    sorry erel you just dont understand me... :( (or i dont understand you, i dont know)
  4. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the logs?
  5. ilan

    ilan Expert Licensed User

    i will connect to my pc and try to make a screen shot (or copy the log and past here)
  6. ilan

    ilan Expert Licensed User

    i will try to explain it again i hope you will understand me.. :confused:

    i have 2 activities: Activity1 called "main", Activity2 called "menu"

    main = only show the logo of my apps and should start activity "menu"
    menu = is the menu of the game where you can choose Play, Exit, Settings,...

    if i exit the game from the MENU activity to Andorid (by clicking the Home button) i want to return again to activity MENU and not to main (where only the logo is shown) thatswhy i call activity.finish in the main activity and thatswhy the timer starts only in firsttime = true because this activity should not be start again

    if i would not finish activity "main" i will go to it if i press the back button in activity "menu" and i dont want it, why should someone see my logo by clicking the back button?!

    so activity "main" is closed and wont load again so it does not matter if the timer is only in firsttime = true

    everything is ok by clicking the Home button on activity "menu" and by returning to the app i get to activity menu (as it should be)
    but if i press back button and the return to app it should return to activity menu like on the home button it does, or am i wrong?

    could it be that it is returning to the main activity and because everything is called on firsttime = true i see a blank screen??? also after i called activity.finish ???
  7. ilan

    ilan Expert Licensed User

    i think i understand now

    back key = userclosed = true
    home button = userclosed = false

    back key will start again activity main also if i have finished it
    home button will return to the same activity it was by leaving it
  8. ilan

    ilan Expert Licensed User

    can i make usercalosed = false also with the back key? because i dont want to return to the 2nd activity, the activity where i pressed back button the leaved the app

    is this possible?
  9. LucaMs

    LucaMs Expert Licensed User

    Even for me, this is not clear, however, I tell you what I would do.

    I might "disable" always the "hardware" back key (ALSO because I read that it will no longer be supported, but I could be wrong) and I would use a button (or whatever) for navigation.

    Also, I would not use the Main just for the logo: I would use your second Activity instead of the Main, by superimposing upon it a splash screen (a panel, setting it to visible/invisible).

    (Do you want to set the variable UserClosed? I think this is not possible.).
  10. ilan

    ilan Expert Licensed User

    ok thanx lucas.

    after understanding a little bit more now i will see how i can fix my apps and rmove Exitapplication because erel said this is not right to do

    but do you know why i should not call activity.finish in activity.pause? and if i wont then where should i call activity.finish?
    i want to start activity 2 from activity 1 and close activity1 so i wont be called again

    i cannot put after:

    Sub b1_click
    Activity.finish >>> this wont be read right? because i start activity menu before it
    end sub

    and like this

    sub b1_click
    startactivity("menu") >>> this wont be read because i finished the activity before this line
    end sub

    so where should i call activity.finish? (my only solution is in activity_pause!) and i need to finish activity main because i dont want to show the logo again if user press back button and i want to use the back button to go back to the android platform
  11. LucaMs

    LucaMs Expert Licensed User

    As always, I'm not sure, without doing some tests (and now I'm not doing).

    If the user presses Home, when he comes back to your app, it should return to the current Activity automatically, I think.

    Do not ask me why, but I think that:

    sub b1_click
    startActivity ("menu")
    Activity.finish '<--- this will be executed
    end sub

    Activity.Finish should be used to close the current activity and return to the previous one, I think. Where to put it depends on the functionality of your activity.
  12. ilan

    ilan Expert Licensed User

    this is what happens

    home button > returns to current (last opened) activity | also userclosed = false
    back button > returns to main | also userclosed = true

    so i guess the app ask if userclosed the app if yes returns to main and start the all app again but if not (like phone call, home button,..) then back to current

    so if i could exit my app and Userclosed will be false i will come back to current activity.

    about the activity.finish, i just put it in activity_pause event and everything is ok but erel said its not correct to do and i would like to understand why

    i guess erel is now in an other univers (IOS) so when he wil land back on B4A he will maybe answer me that question (i hope) :)
  13. Erel

    Erel Administrator Staff Member Licensed User

    Yes, you are wrong. When the back key is pressed the activity is destroyed and removed from the stack. Later when the user returns to your app the main activity is created (and FirstTime is false if the process was not killed).

    The solution is very simple. Just call StartActivity(menu) if FirstTime is false.
  14. Erel

    Erel Administrator Staff Member Licensed User

    BTW, this is exactly why I asked you to post the logs. It shows you which activity was created.
  15. ilan

    ilan Expert Licensed User

    ok thank you i learned something new today

    the difference between back key and home key

    is there a way to exit my app with the back key but dont remove it from the stack?
  16. Erel

    Erel Administrator Staff Member Licensed User

    You should go over the tutorial in the first post. The back key behavior is clearly explained there.

    You can catch the back key and show the home screen instead of closing the activity. Though your app should know how to handle this case correctly.
  17. Kwame Twum

    Kwame Twum Active Member Licensed User

    Hi everyone, I have a fair understanding of the LifeCycle concept, but my app is behaving strangely...
    I'm using RSCrop Library(by @XverhelstX) to load an image from the gallery and crop it. This image would then be set as the background of an imageview.
    These are the steps I've already proposed to go about:
    1. User taps on the ImageView
    2. RSCrop Starts the gallery (Activity Pauses)
    3. Image is selected and cropped
    4. Activity resumes and the cropped image is set as the background of an imageview.
    However, my paused Activity most often never resumes and sometimes, even kills the entire app. The Cropped Sub is sometimes never reached/called. I think the OS might be killing it or something else is wrong, but I'm not sure why...

    Need help :rolleyes:

    Testing Devices:
    1. Emulator (Android 2.3.3) - Happens once in a while
    2. Samsung Galaxy Ace 3 (Android 4.1.2) - Happens almost everytime :(
    3. Sony Xperia Sola (Android 4.4.2) - Rarely happens
  18. Erel

    Erel Administrator Staff Member Licensed User

    This can happen on devices with low memory. The solution is to start a service and call Service.StartForeground before opening the gallery.
    Kwame Twum likes this.
  19. Kwame Twum

    Kwame Twum Active Member Licensed User

    Worked for me, thanks @Erel
  20. John Woodsmall

    John Woodsmall Active Member Licensed User

    in b4a i can:

    how do i do this in b4i?
  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