Simple callback interface instance and callback instance
Callback Interface
<Span style = "font-size: 18px;">/*** callback interface * @ author Chillax K. */public interface ReCallBack {public void doSomething () ;}</span>
Implementation class
<Span style = "font-size: 18px;">/*** callback interface * @ author Chillax K. * // @ TargetApi (Build. VERSION_CODES.HONEYCOMB) public class MyFragment extends Fragment {public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view View = inflater. inflate (R. layout. message_layout, container, false); view. findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Overridepu Blic void onClick (View arg0) {// TODO Auto-generated method stubToast. makeText (getActivity (), "click the callback button to call the callback function", 0 ). show (); if (myReCallBack! = Null) {myReCallBack. doSomething () ;}}); return view;} private ReCallBack myReCallBack; public void setMyReCallBack (ReCallBack myReCallBack) {this. myReCallBack = myReCallBack; }}</span>
Called back
<Span style = "font-size: 18px;">/*** callback interface * @ author Chillax K. * // @ TargetApi (Build. VERSION_CODES.HONEYCOMB) public class MainActivity extends Activity {/*** Fragment used to display messages */private MyFragment messageFragment;/*** used to manage Fragment */private FragmentManager fragmentManager; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // you can specify the value of requestWindowFeature (Window. FEATURE_NO_TITLE); // set full screen // getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, // WindowManager. layoutParams. FLAG_FULLSCREEN); setContentView (R. layout. activity_main); fragmentManager = getFragmentManager (); FragmentTransaction transaction = fragmentManager. beginTransaction (); messageFragment = new MyFragment (); // sets the callback variable messageFragment. setMyReCallBack (mReCallBack); transaction. add (R. id. content, messageFragment); transaction. commit ();} ReCallBack mReCallBack = new ReCallBack () {@ Overridepublic void doSomething () {// TODO Auto-generated method stubToast. makeText (getBaseContext (), "Callback MainActivity", 0 ). show () ;};</span>
The code is relatively simple. If you do not understand it, you can look at the source code.