Fragment and Acitvity Communication in Android development

Source: Internet
Author: User

We talked about the common functions related to fragment, since fragment is called "small activity", now let's talk about how fragment communicates with Acitivity. If you do not know the last article, you can look again. Transmission door.

Fragment communicate with the activity in the following ways:

One, through the initialization function provides

1. In the process of dynamically adding fragment, we provide data for fragment in activity by means of fragment.setarguments ();

2. In fragment, a bundle object is obtained by invoking Getarguments () in the Onattach () function to obtain the data we provide.

Second, create callback interface

For example: In the news browsing situation, there are two fragment, one is used to display news headlines, and another to display news content. When we click on the headline, the content of the news is displayed in another fragment. Here we will pass a message to the Fragment2 by pressing the button in the Fragment1.

The first thing we have to do is create a callback interface in the Fragment1 and rewrite his method in the activity, passing the message to Fragment2 (extrapolate: We can also pass it to the other fragment).

Below I give the FRAGMENT1 code:

Package com.example.fragment;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
    
Import Android.view.ViewGroup;
    
Import COM.EXAMPLE.FRAGMENTDEMO.R;
    public class Fragment1 extends Fragment {private static final String TAG = "Fragmentdemo";
    
    Private Onbuttonclicklistener Mlistener; /** * Here we create a callback interface * * * @author MYP * */public interface Onbuttonclicklistener {PU
    Blic void onbuttonclicked ();
        @Override public void Onattach (activity activity) {LOG.V (TAG, "Fragment1 Onattach");
        Bundle args = Getarguments ();
             if (null!= args) {/* * Here we can save the data received from acivity and display it in fragment. * * LOG.D (TAG, "Fragment1 GeT data from the activity "+ args.getstring (" Hello "));
        try {Mlistener = (onbuttonclicklistener) activity;  catch (ClassCastException e) {throw new ClassCastException (activity.tostring () + must
        Implement Onbuttonclicklistener ");
    } super.onattach (activity); @Override public void oninflate (activity activity, AttributeSet attrs, Bundle savedinstancestat
        e) {log.d (TAG, "oninflate");
    Super.oninflate (activity, attrs, savedinstancestate);
        @Override public void OnCreate (Bundle savedinstancestate) {log.v (TAG, Fragment1 onCreate);
    Super.oncreate (savedinstancestate);
        @Override public void OnDestroy () {LOG.V (TAG, "Fragment1 OnDestroy");
    Super.ondestroy ();
        @Override public void Onresume () {LOG.V (TAG, "Fragment1 onresume"); SupeR.onresume ();
        @Override public void OnStart () {LOG.V (TAG, "Fragment1 OnStart");
    Super.onstart ();
        @Override public void Ondetach () {LOG.V (TAG, "Fragment1 Ondetach");
    Super.ondetach ();
        @Override public void OnPause () {LOG.V (TAG, "Fragment1 onpause");
    Super.onpause ();
        @Override public void OnStop () {LOG.V (TAG, "Fragment1 onStop");
    Super.onstop (); @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedin
        Stancestate) {log.v (TAG, "Fragment1 Oncreateview");
        View view = Inflater.inflate (R.layout.fragment1, container, false); Registers the event for the button and invokes the callback interface to return the information to the upper View.findviewbyid (r.id.fragment1_btn). Setonclicklistener (New OnC
                    Licklistener () {@Override public void OnClick (View v) {    Mlistener.onbuttonclicked ();
        }
                });
    return view;
        @Override public void Ondestroyview () {LOG.V (TAG, "Fragment1 Ondestroyview");
    Super.ondestroyview (); @Override public void onactivitycreated (Bundle savedinstancestate) {log.v (TAG, "Fragment1 onactivi
        Tycreated ");
    Super.onactivitycreated (savedinstancestate); }
    
}

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.