In-depth analysis: fragment and Activity Interaction methods (3) use interfaces)

Source: Internet
Author: User

Step 1: Define an interface in fragment and make sure that our container activity implements this interface:


Public interface ontestlistener {
Public void ontest (string Str);
}< br> @ override
Public void onattach (activity) {
super. onattach (activity);
// This method is used to check whether the current activity container inherits this interface, if no exception is thrown,
try {
mcallback = (ontestlistener) activity;
} catch (classcastexception E) {
throw new classcastexception (activity. tostring ()
+ "must implement onheadlineselectedlistener ");
}< br> }< br>

Step 2: implement this interface in our container activity and implement the methods in the interface.

Public class mainactivity extends activity implements ontestlistener {

......

@ Override
Public void ontest (string Str ){
Text. settext (STR );
}

Step 3: Call the methods declared in the interface when we perform certain operations in fragment.

@ Override
Public void onclick (view v ){
Mcallback. ontest (edit. gettext (). tostring ());
}


Link to the complete code: http://download.csdn.net/detail/huangyabin001/7560291click to open the link

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.