0

I was trying to understand the flow of events when onDatachanged is called in android.According to me the ideal flow of the program should first print "Inside onclick" then "inside Ondatachanged" and the "endof of onclick" however the flow that I get is random, for the second and third line.Can anyone please explain me how do I make the flow linear, and why is it non linear?Also, when I putstring data the first time, Ondatachanged is not called.Why is that?

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setAmbientEnabled();
    //Google client
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d("Inside", "onConnected " );


                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d("Inside", "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d("Inside", "onConnectionFailed: " + result);
                }
            })
            // Request access only to the Wearable API
            .addApiIfAvailable(Wearable.API)
            .build();
    mGoogleApiClient.connect();
    Intent service= new Intent(this,WearbleService.class);
    startService(service);
    Wearable.DataApi.addListener(mGoogleApiClient, this);
    //Listener for button
    Button done=(Button)findViewById(R.id.button);
    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Inside","Onclick");
            PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
            int count=0;
            putDataMapReq.getDataMap().putString("key", "HelloWOrld");
            PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
            PendingResult<DataApi.DataItemResult> pendingResult =
                    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
           // putDataMapReq.getDataMap().putString("key", "helloWorld");
            putDataReq = putDataMapReq.asPutDataRequest();
            pendingResult =
                    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
            Log.d("End of ", "Onclick " );

        }
    });

  @Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d("Inside","OndataChanged");
}
UndoTheUndo
  • 43
  • 10

0 Answers0