Simple callback interface instance and callback instance

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.