How to Define Callbacks in Android?

后端 未结 6 1102
太阳男子
太阳男子 2020-11-22 05:32

During the most recent Google IO, there was a presentation about implementing restful client applications. Unfortunately, it was only a high level discussion with no source

6条回答
  •  日久生厌
    2020-11-22 05:59

    When something happens in my view I fire off an event that my activity is listening for:

    // DECLARED IN (CUSTOM) VIEW

        private OnScoreSavedListener onScoreSavedListener;
        public interface OnScoreSavedListener {
            public void onScoreSaved();
        }
        // ALLOWS YOU TO SET LISTENER && INVOKE THE OVERIDING METHOD 
        // FROM WITHIN ACTIVITY
        public void setOnScoreSavedListener(OnScoreSavedListener listener) {
            onScoreSavedListener = listener;
        }
    

    // DECLARED IN ACTIVITY

        MyCustomView slider = (MyCustomView) view.findViewById(R.id.slider)
        slider.setOnScoreSavedListener(new OnScoreSavedListener() {
            @Override
            public void onScoreSaved() {
                Log.v("","EVENT FIRED");
            }
        });
    

    If you want to know more about communication (callbacks) between fragments see here: http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

提交回复
热议问题