1. Check out our companion app, Forums for Android! Download from Google Play

The Life of an Android Application ??

Discussion in 'Application Development' started by xmiasma, Mar 30, 2010.

  1. xmiasma

    xmiasma Member
    Thread Starter

    Dec 28, 2009
    Whats up guys I am new to the android app thing and there one thing that i need to understand. im very familiar with c/c++ and the whole idea that there is a main loop that does stuff like updating rendering and all that. But as i was reading the android developer site i saw that they dont have a main() but rather it seems that the way it switches from task to task is by activities(i think?).

    Ill make a simple example so i can show you what im trying to say. Lets say your writing a game (thats what i want to do) and you have the main screen and then you have ur updates for anims, data, and game logic followed by rendering. So how do all these work together? do you just call the startActivity(next state) at the end of each function?? for example

    main_menu - if they press start game -->startActivity(newGame)
    *game is now running*
    Now game is a new activity is guess? that has a main loop which calls the updates and render?
    then at the end of update it switches back to the game
    game then calls its render
    go back and do the loop over

    Sorry if its so long but if you read it thanks alot and just leave a message that can help me understand this.


  2. markb

    markb Well-Known Member

    Sep 14, 2009
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK

    Android will provide a GUI thread for your app automatically.
    So you can register an interest in events, and it will call your code when those events happen.

    You don't have to have a loop that does the updates and the rendering.
    You might want to consider using a timer thread. Although a loop that sleeps most of the time, and wakes up at regular intervals when it needs to do something, will produce the same result.

    You can arrange for your code to be called at regular intervals, and when your code is called you can then do your game logic, updates and rendering. Depending on the type of game, you could drive your game logic using the timer interval. So if you had a timer that called your code every 0.1 seconds, for example, then you could move all your players by the distance they'd move in 0.1 seconds, based on their speed.

    Driving the logic based on time intervals will lead to consistent performance across different devices. If you did a loop, and updated everything as fast as possible, then it might look OK on a Nexus One, but be really jerky on a slower phone such as the G1.

    So, in very high level terms, your app would work something like this:

    App starts, and your MainActivity in created.
    In your MainActivity you show a menu, or whatever you like. You register event handlers to wait for a button press (for example) to start the game.

    When the button is pressed, you start a new Activity: GameActivity.
    In GameActivity you set up any buttons, drawing surfaces, etc, and register event handlers, and also start your GameThread, which will be responsible for doing updates at regular intervals.

    I intended to write more, but it's 2am here, and I have to go.
    If that's any help to you then I could add a bit more detail if you like.

  3. KlaymenDK

    KlaymenDK Well-Known Member

    May 29, 2009

Share This Page