views:

131

answers:

1

In an Activity, I have some snippet of code that fires off a Message:

        Message m = _handler.obtainMessage( UPDATE_PROGRESS );
        Bundle bundle = new Bundle();
        bundle.putInt( BUNDLE_KEY, 50 );
        m.setData( bundle );
        Log.d( LOG_TAG, "value " + bundle.getInt( BUNDLE_KEY, 0 ) ); // prints 50
        Log.d( LOG_TAG, "sent:" + _handler.sendMessage( m ) ); // prints true

In another part of the Activity, I am handling it like this:

    public void handleMessage( final android.os.Message msg ) 
    {
        switch( msg.what )
        {
        case UPDATE_PROGRESS:
            post( new Runnable() 
            {
                @Override
                public void run()
                {
                    Bundle aBundle = msg.getData();
                    Log.d( LOG_TAG, "value in bundle=" + 
                         aBundle.getInt( BUNDLE_KEY, 0 ) ); // prints 0

                    Log.d( LOG_TAG, "contains 'progress' : " + 
                         aBundle.containsKey( BUNDLE_KEY ) ); // prints 'false'

                }
            });
            break;
          }

Why is the Bundle not preserved in the Message ? Who is resetting the values of the Bundle ?

A: 

The problem is solved. Basically in the handleMessage method, don't wrap the lambdas inside a Runnable. Execute your modifications in-situ.

Jacques René Mesrine