1. Are you ready for the Galaxy S20? Here is everything we know so far!

Understanding Android life cycle

Discussion in 'Android Development' started by RhinoCan, Aug 4, 2018.

  1. RhinoCan

    RhinoCan Well-Known Member
    Thread Starter

    What is my best way to learn and properly understand the Android life cycle?

    I used to develop Java applications for desktop use on Windows and got quite used to putting an Exit button on every program so that the user could leave the program when they are done; that would inevitably cause a System.exit(0) when it was detected. But I learned last night that this is NOT appropriate for the Android environment via a StackOverflow discussion I found: https://stackoverflow.com/questions...android-studio-and-an-application-project-tha

    Now, that discussion is focused mostly on pleasing users that expect an Exit button (or menu option) and I largely understand the reasons why this may not be appropriate in Android. But it raises the question of what I'm supposed to do if the app encounters a problem that means it should not proceed? If System.exit(0) or some of the other alternatives proposed in that discussion are off the table, then what SHOULD my app do?

    For instance, one possibility that I can easily imagine is a situation where my app, which shows a list of sales, is unable to find any sales on the database. Normally, that shouldn't happen but I can see two situations where it might: the database itself is offline for some reason or the database is online but simply has no data in it (probably because I deleted it for some reason to do with testing and forgot to restore it). In either case, with none of the existing data available, there is none to show the user and the app no longer has much purpose. What SHOULD happen then? If the database is down, I would think I'd want to explain to the user via an AlertDialog that the database is down and the app will be unusable until the database is back up and tell them that the "system administrator" (that's me) has been notified and then have the program send me a text. At that point, I was going to close the app but the discussion I've cited effectively says it's just going to stay around until the memory is needed for something else. Well, if the app is still running, my two users may continue trying to use it and frustrating themselves. Do I just keep popping up that AlertDialog and sending texts to the poor old system administrator?

    Also, for the case where the data is simply gone because I deleted it and forgot to restore it (mind you, I don't expect that to happen once I'm finished testing!), do I notify the user but disable the buttons that they normally click on to do things like change or delete sales pending the restoration of the data?

    Ultimately though, I suspect that it would be best if I properly understood the Android life cycle in general rather than peppering this forum with questions about specific situations like mine. What's the best way of getting at least the basics of the life cycle?

  2. Best Answer:
    Post #2 by Deleted User, Aug 4, 2018 (1 points)
  3. Deleted User

    Deleted User Guest

    See here


    The Android app lifecycle revolves around the Activity. Since the main Activity class is the entry point to your app.
    An Android app isn't a stand alone process in the same sense that a Java desktop app would be. It doesn't have a main() method. Rather it's a thread of execution within the Android VM (Dalvik), which is managing all apps currently running on the device as separate threads of execution. The Activity's onCreate() method is the entry point for your app. As you can see from the lifecycle diagram in the above link, there are other callback methods which get invoked at various points.

    As to what should happen when your app encounters an error condition, well that's entirely up to you. You'll almost certainly want to inform the user. But as to what other action is required, that depends on what your app is trying to do, and what makes sense to you.

    You'll notice from the diagram that the Activity has various states, including being suspended, or going into the background. You're not required to manage these state transitions, as that's the job of the runtime framework. However you do get the chance to do something, by writing code for the various methods, such as onPause(). For example, you may wish to manually close down any open network connections.
  4. RhinoCan

    RhinoCan Well-Known Member
    Thread Starter

    Thanks for the starting point. I usually find the Android Developer writeups unsatisfying but I'll start there anyway; if I find myself wanting more, I'll start googling....

Share This Page