Andriod fragment calls the Activity function method, andriodactivity

Source: Internet
Author: User

Andriod fragment calls the Activity function method, andriodactivity

 

(1) Add an activity

1 package com. xxxx. activity; 4 5 public interface FragmentCallBack {7 8 public MainActivity getMainActivity (); 9 10}

 

(2) Add the getMainActivit public method to the MainActivity Java file.

1 @ Override2 public MainActivity getMainActivity () {3 return this; 4}

 

(3) Call all public methods for accessing MainActivity in fragment;

1 // Declaration: Pay attention to reference 2 private FragmentCallBack mFragmentCallBack; 3 4 5 // creation: 6 @ Override 7 public void onAttach (Activity activity) {8 super. onAttach (activity); 9 try {10 mFragmentCallBack = (FragmentCallBack) activity; 11} catch (ClassCastException e) {12 throw new ClassCastException (activity. toString () 13 + "must implement OnHeadlineSelectedListener"); 14} 15} 16 17 18 // call: 19 mFragmentCallBack. getMainActivity (). showmsg ();

 

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.