Android callback

July 20, 2012   ·   0 Comments

Screen Shot 2012-07-20 at 12.42.03 AM

// The callback interface
interface MyCallback {
    void callbackCall();
}

// The class that takes the callback
class Worker {
   MyCallback callback;

   void onEvent() {
      callback.callbackCall();
   }
}

// Option 1:

class Callback implements MyCallback {
   void callback() {
      // callback code goes here
   }
}

worker.callback = new Callback();

// Option 2:

worker.callback = new MyCallback() {

   void callback() {
      // callback code goes here
   }
};

sample code: Tutorial_CallbackActivity

package com.poweko.Tutorial_Callback;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Tutorial_CallbackActivity extends Activity  implements MyCallback {

	   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        

        test t_test=new test();
     // solution 1
        t_test.callback = new Tutorial_CallbackActivity();   //<--
        t_test.onEvent(); 
        
        
        
        	
    }

	@Override
	public void callbackCall() {
		Log.d("callback", "I get it");
		
	}

}
package com.poweko.Tutorial_Callback;

interface MyCallback {
    void callbackCall();

}
package com.poweko.Tutorial_Callback;

public class test {
	  MyCallback callback;

	   void onEvent() {
	      callback.callbackCall();
	   }
}

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon