Android callback?

In Android app development, I often repeat the word CallBack in many places. I want to know what it means to tell us technically - and how I can use CallBack in applications. I need guidance to understand and use it.

+14
android android-emulator
Apr 2 '10 at 13:30
source share
3 answers

I want to know, I want it means to tell us technically

http://en.wikipedia.org/wiki/Callback_%28computer_science%29

"In object-oriented programming languages ​​without function-specific arguments, such as Java, [callbacks] can be modeled by passing an abstract class or interface from which the receiver will call one or more methods, while calling the specific implementation. Such objects actually represent is a set of callbacks, as well as the data they need to manipulate. They are useful in implementing various design patterns such as Visitor, Observer and Strategy. "

how can i manage the application callback

I have no idea what that means.

+13
Apr 02 '10 at 13:35 on
source share

Hm. How about an example. You write a quick sort algorithm in C. A user who wants to use your algorithm must provide a comparison method suitable for the user to sort with your algorithm. The user must pass a pointer to a method for comparing users with your quick sort code. Quicksort code uses this address, a function pointer, to call the CALL BACK function for the user. You provide a function prototype, no implementation, because you cannot know how to determine the routine of what is sorted. The user supplies a comparison implementation that makes sense for the user to sort. This implementation must match the function prototype. The function pointer is used by the quicksort agorism to go back and touch the user code.

It is actually about polymorphism.

In java you can use an interface for this. Therefore, for sorting, see IComparer and IComparable Interface.

+5
Feb 19 '11 at 2:44
source share

The arbitrary interface can be used to run a piece of code, as Runnable does. However, Callable may return a result and may throw a checked exception.

More details. http://developer.android.com/reference/java/util/concurrent/Callable.html

Using Callable interfaces, you can pass an argument as a function, I added a simple code snippet for understanding.

 public class MainActivity<V> extends Activity { Callable<String> doLogin=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); doLogin=new Callable<String>() { //created but not called now. @Override public String call() throws Exception { //make some piece of code return "something"; //or false } }; CheckSession checkSession=new CheckSession("sessionName"); String sessionKey=""; try { //we are sending callable to the DAO or any class we want sessionKey=checkSession.getSessionKey(doLogin); } catch (Exception e) { e.printStackTrace(); } } } public class CheckSession{ String sessionName=""; Callable<String> func=null; public CheckSession(String sessionName) { super(); this.sessionName = sessionName; } public String getSessionKey(Callable<String> doLogin) throws Exception{ func=doLogin; return (String) func.call(); } } 
+4
Feb 17 '15 at 13:13
source share



All Articles