1

i have been looking for and i found this ...

 interface CallBack {
    void methodToCallBack();
}

    class CallBackImpl implements CallBack {
        public void methodToCallBack() {
            System.out.println("I've been called back");
        }
    }

    class Caller {

        public void register(CallBack callback) {
            callback.methodToCallBack();
        }

        public static void main(String[] args) {
            Caller caller = new Caller();
            CallBack callBack = new CallBackImpl();
            caller.register(callBack);
        }
    }

what's difference with this other.

interface CallBack {
    void methodToCallBack();
}

class CallBackImpl implements CallBack {
    public void methodToCallBack() {
        System.out.println("I've been called back");
    }
}

class Caller {

    public static void main(String[] args) {

        CallBack callBack = new CallBackImpl();

        callBack.methodToCallBack();
    }
}

my situation: I have a onclicklistener (only a child not complete row) within of adapter y when this listener execute ,i wanna execute a method on activity because in activity i have access UI..

kcire_eae
  • 333
  • 1
  • 3
  • 10

3 Answers3

1

Purpose of callbacks is for asynchronous communication. You tell somebody that let me know when something happened through callback (phone number). Apart from this, the advantage I felt with callbacks is that:
- you can avoid dependency between the classes.
- helps avoiding memory leaks.

Take an example of communication from fragment to activity. Though you can call getActivity().aMethodInActivity() it is not preferable because it violates one of the purpose of fragments that is ''reusability'. And we may tend to pass activity instance to fragment which may lead to activity leaks.

Same is the case with fragments and adapters. Communication from adapter to fragment should happen using callbacks. Otherwise if fragment is passed as parameter to adapter, adapter is holding reference to fragment and fragment is holding the reference of adapter. This may lead to leaks.

I tried to explain the advantage of callbacks from android perspective with couple of examples I know. You can explore further.

cgr
  • 4,578
  • 2
  • 28
  • 52
  • ok,thank you for example, i would apreciate if help me to implement callback between adapter and activity... – kcire_eae Dec 02 '15 at 21:10
  • You may vote up too. It is exactly same as you implement callback between Fragment and Activity. In fact implementing callback is same every where. The class that has to be notified should implement interface and pass the instance to caller class. Follow this for your case http://stackoverflow.com/a/12142530/3209739 – cgr Dec 02 '15 at 21:17
  • thank you, and i sorry for my english,but the results in spanish are very poor. So try to search on stackoverflow, thank you very much for you answer. – kcire_eae Dec 02 '15 at 21:27
  • I understnad, not a problem. I suggest you use translator and then search with english sentence. Also you can vote up my answer by clicking up arrow if you like it. – cgr Dec 02 '15 at 21:32
  • i would like do it ,but i don't have enough reputation to vote up. Even so i appreciate your answer was useful – kcire_eae Dec 02 '15 at 21:38
0

With the first method, you register to the Callback, so methodToCallBack can be called automatically.

u say you have an OnClickListener, i guess it is in another class. You can create it as inner class, so you will have acces to your UI.

Or you give your OnClickListener everything he needs in the Constructor

MeGoodGuy
  • 395
  • 2
  • 10
0

This should do exactly what you want : Communicating with Other Fragments

lee
  • 53
  • 5