The mutual Communication instance code between the Android fragment and the activity _android

Source: Internet
Author: User

Mutual communication between Android fragment and activity

Objective

Since the introduction of Android3.0, the main purpose of the fragment is to support dynamic and flexible interface design and screen adaptation problems. Fragmenty cannot exist alone and must rely on activity as part of a view display, a colleague who has its own lifecycle, receives its own events, has more flexible features, and today fragment has been widely used in app development, The most common is the single activity multiple fragment mode. Fragment relies on activity and is unavoidable to interact with the activity, and here is a simple introduction to three common ways of interacting with each other.

One, interface mode

Define an interface in the fragment class and implement it in the activity. Fragment the object that gets the concrete implementation of the interface in the Onattach () callback function. Later, fragment can invoke the methods in the interface to communicate with the activity.

After Android5.0, the method of Onattach (activity activity) is abolished, and the Onattach (context) is used, which means that the parameters passed are changed

public interface showmsglistener{
    void showmsg (String str);
  }

  Private Showmsglistener Msglistener;

  @Override public
  void Onattach [activity activity] {
    Super.onattach (activity);
    try {
      if (activity!=null) {
        msglistener= (mainactivity) activity;
      }
    catch (ClassCastException e) {
      throw new ClassCastException (activity.tostring ()
          + "must implement Showmsglistener");
    }

    Mbutton.setonclicklistener (New View.onclicklistener () {
      @Override public
      void OnClick (view view) {
        Msglistener.showmsg ("Hello android!" ( interface);
    

This interface needs to be implemented in the activity

 @Override public
  void ShowMsg (String str) {
    toast.maketext (mainactivity.this, str, toast.length_short). Show ();
  }

Second, broadcast

Registering a broadcast receiver in an activity, sending a broadcast in fragment

  Private Broadcastreceiver Mbroadcastreceiver = new Broadcastreceiver () {
    @Override public
    void OnReceive ( Context context, Intent Intent) {
      String action = intent.getaction ();
      if (Action.equals (Action_name)) {
        String msg = Intent.getstringextra ("msg");
        Toast.maketext (Mainactivity.this, MSG, Toast.length_short). Show ();}}
  ;

  public void Registerboradcastreceiver () {
    Intentfilter myintentfilter = new Intentfilter ();
    Myintentfilter.addaction (action_name);
    Registerreceiver (Mbroadcastreceiver, myintentfilter);
  }

  @Override
  protected void OnDestroy () {
    Super.ondestroy ();
    Unregisterreceiver (Mbroadcastreceiver);
  }

Third, Fragment from the activity to obtain data

Set the parameters in the getinstance () method of the Fragment () setarguments ()

public static Fragment getinstance (String msg) {
    Bundle Bundle = new Bundle ();
    Bundle.putstring ("msg", msg);
    Mainfragment fragment=new mainfragment ();
    Fragment.setarguments (bundle);
    return fragment;
  }

The Getarguments () method is then used to obtain

 public void Initdatas () {
    Bundle bundle=getarguments ();
    if (bundle!=null) {
      String msg=bundle.getstring ("msg");
      Toast.maketext (Getactivity (), MSG, Toast.length_short). Show ();
    }
  

Thank you for reading, I hope to help you, thank you for your support for this site!

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.