views:

201

answers:

1

I slapped together a simple test application that has a button, and makes a noise when the user clicks on it. Here are it's method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b = (Button)findViewById(R.id.easy);
    b.setOnClickListener(this);
}

public void onClick(View v) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.easy);
    mp.start();
    while(true) {
        if (!mp.isPlaying()) {
            mp.release();
            break;
        }
    }
}

My question is, why is onCreate acting like it's in a while loop? I can click on the button whenever, and it makes the sound. I might think it was just a property of listeners, but the Button object wasn't a member variable. I thought that Android would just go through onCreate onse, and proceed onto the next lifecycle method.

Also, I know that my current way of seeing if the sound is playing is crap...I'll get to that later. :)

Thank you.

+1  A: 

You're thinking of the button instance in terms of the code you've written as opposed to an instance that lives in a view that the framework has constructed. Your Button variable (b) is simply a reference to that. Your reference is no longer alive once the onCreate method goes out of scope, but the button still exists in your UI. Your reference was around long enough to attach a listener method to it, so for the lifetime of the button (that you still see in the UI after the onCreate has come and gone, obviously) is more associated with when you can see it and not the scope of the variable you created.

Btw...I know you're just getting started and your MediaPlayer code was just thrown together, but for future reference, you don't need a while loop. MediaPlayer gives you an onCompletionListener callback:

OnCompletionListener

Rich
It's not that "you don't need a while loop" -- having that while loop will tie up the main application thread, freeze your user interface, and eventually cause Android to kill off your activity.
CommonsWare
Ah, thanks for the method. :)Anyway, so you're saying that I removed the pointer, but not the object itself, and because the UI keeps a reference to the button, the JVM (or whatever we call the android version of the JVM), keeps it around. If that's a case, so this means that the button is going to last until the app closes, or just until the activity ends (or it goes off of the screen)? Again, thanks.
Leif Andersen
It just means, don't think of the lifetime of your button as the same as the lifetime of a variable that points to it. The mechanisms within the Android framework that constructed your UI (in this case, building out an object graph from an xml file you created and displaying it on the screen) control the lifecycle of the objects it creates. In this case, the Activity owns it and I would imagine it's in scope only as long as it's viewable. Likewise, your listener object is in scope as long as there is a reference to it.
Rich