Why is BluetoothChat synchronized by the ActivityResume lifecycle method?

I'm learning right now the Android Bluetooth interface, and I came across an example of BluetoothChat. http://developer.android.com/resources/samples/BluetoothChat/index.html

It contains a lot of errors, first of all the simple fact that it uses API 11, but the manifest does not force this minimal API.

Another interesting is the use of a synchronized keyword in activity lifecycle methods, for example on onResume:

    @Override
public synchronized void onResume() {
    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");

    // Performing this check in onResume() covers the case in which BT was
    // not enabled during onStart(), so we were paused to enable it...
    // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
    if (mChatService != null) {
        // Only if the state is STATE_NONE, do we know that we haven't started already
        if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
          // Start the Bluetooth chat services
          mChatService.start();
        }
    }
}

Why is this keyword used there? Is there a reasonable explanation, or just the one who wrote the code did not know that onResume will always be called by the same thread? Or am I missing something?

Thank you in advance!

+5
1

, , , , , :

, , "". BluetoothChat ( ) Bluetooth, /.

, , , onResume.

, , , , synchronize . , , - , onResume;

- :

//class fields    
private Object myLockObj = new Object();
private boolean isPausedForPairing = false;

public void onResume()
{
    super.onResume();

    synchronized (myLockObj)
    {
        if (isPausedForPairing)
        {
            //handle a "pairing" onResume
        }
    }
}

- , , , , - . , , . "" . , , .

+1

All Articles